Is there any special reason? I know that that's how the language has been written, but can't we change it? And what are the challenges we'd face if the index start with 1?
-
3You forgot Eiffel, Smalltalk, and Prologue in your language selections for this. – WhozCraig Aug 07 '13 at 17:18
-
6[Why numbering should start at zero : Dijkstra](http://www.cs.utexas.edu/users/EWD/ewd08xx/EWD831.PDF) – Ashwini Chaudhary Aug 07 '13 at 17:18
-
1Why doesn't it begin with -1? – BenDundee Aug 07 '13 at 17:19
5 Answers
For historical reasons, and reasons connected to how an array is "made" in memory.
In C, an array is a piece of memory (with some information at compiler level on its size). You have a pointer (a reference) to its first element. To go to its second element you can do
int array[10]; // your array
int *p = array; // reference to first element of array
int *q = p + 1; // reference to second element of array
int *r = p + 2; // reference to third element of array
clearly, for symmetry:
array[0] // reference to first element of array
array[1] // reference to second element of array
array[2] // reference to third element of array
the [x]
operator of C is in fact compiled as array + x
.
You see? Array are "base 0" in C. And for this reason in many other languages it's the same.
Now, C++ has its roots in C, Java has its roots in C++ and other languages, C# has its roots in C++, java and other languages... Same tree.
Basic clearly is another tree :-)

- 109,618
- 12
- 197
- 280
-
Great, that you mentioned "in many other language", because 0-based indices are not a law graven in stone. – Hyperboreus Aug 07 '13 at 17:21
-
1
-
-
The basic reason behind it is that the computer remembers the address at wich the first part of any variable/object is stored. So the index represents the "distance" in between that and what you're looking for, so the first one is 0 away, the second 1...

- 71
- 1
- 5
In C and C++, array indexing is syntactic sugar for dereferencing an offset pointer. That is,
array[i]
is equivalent to *(array + i)
. It makes sense for pointers to point to the beginning of their block of memory, and this implies that the first element of the array needs to be *array
, which is just array[0]
.

- 26,823
- 32
- 143
- 200
Good question. Actually, almost all programming language implementations start indexing at 0.
While you could potentially use workarounds to make it seem like they're starting with 1, don't.
Dijkstra's article will give a much better defense of zero based arrays than I can:
http://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EWD831.html

- 3,056
- 1
- 21
- 32
One thing is that, you can reference and navigate the arrays using pointers. Infact the the array operations decay to pointer arithmetic at the back end.
Suppose you want to reach a nth
element of an array then you can simply do (a + n)
where a
is the base address of an array(1-dimension), but if the subscript starts at 1
then to reach the nth
element you would have to do (a + n -1)
all the time.
This is because just by taking the name of an array you the the address of the starting element of it, which is the simplest way!

- 5,287
- 2
- 23
- 26