Why does the indexing start with zero in 'C'?

前端 未结 16 2295
执念已碎
执念已碎 2020-11-22 16:22

Why does the indexing in an array start with zero in C and not with 1?

16条回答
  •  粉色の甜心
    2020-11-22 16:44

    Because 0-based index allows...

    array[index]
    

    ...to be implemented as...

    *(array + index)
    

    If index were 1-based, compiler would need to generate: *(array + index - 1), and this "-1" would hurt the performance.

提交回复
热议问题