3

I have a question on C/C++ arrays.

Why does the index of the arrays start from '0' not from '1'?

Is there any mathematical reason for that?

  • 3
    This is a duplicate of [Why are structs/arrays zero based?](http://programmers.stackexchange.com/q/70612/1033) on Programmers SE. – mouviciel Jun 03 '13 at 08:40
  • @ring0: Thanks for the post, I would have done a better search before posting. Thanks anyway. –  Jun 03 '13 at 08:41

1 Answers1

29

In most programming language, the name of any array is a pointer, which is nothing but a reference to a memory location, and so the expression array[n] points to a memory location which is n-elements away from the first element. This means that the index is used as an offset. The first element of the array is exactly contained in the memory location that array points to (0 elements away), so it should always be referred as array[0].

a[i] can also be read as value at [a+i] which is denoted as *(a+i) , so it always starts at zero.

Vivek Sadh
  • 4,230
  • 3
  • 32
  • 49