About index starting from zero

Is there any detailed mathematical meaning or purpose for the index of array starting from zero and ends in (n-1) instead of starting from 1 and ends in n?

Now I only know that the array's name is the address of its start point and it is convenient to set it as index 0 and let all the other elements' index as how many offsets it is from the start point. But it seems that it is not the only reason for
setting index starting from zero.

Thank you!
If you have a pointer p to the first element in an array,
and you want to access the element at index i,
then you would write p[i] which has the meaning *(p + i).

If indices started at 1 instead of 0 the meaning of p[i] would instead have to be *(p + i - 1),
which would be unnecessary computations and quite confusing.

There are languages where indices start at 1 (e.g. MATLAB) but those are usually a bit more high-level and can hide what is really going on.
Last edited on
An answer is that zero-indexing is how CPU hardware does it, and C is close to the hardware, C++ came from there. It would of course be possible to have the compiler silently subtract 1 from everything at compile time for you, but that just makes the compiler that much slower.

if it bothers you that much..

char hw[] = "Hello World!";
char* hwp = &(hw[-1]); //if this is not supported on your compiler, you can -1 the pointer instead.
cout << hwp[1]; //H
I do not recommend this, but had to translate matlab to C and this kind of thing works if you are trying to translate a lot of code efficiently.
Last edited on
It would of course be possible to have the compiler silently subtract 1 from everything at compile time for you, but that just makes the compiler that much slower.

I don't see how it could be done at compile when the array addresses and indices might only be known at runtime.
> Is there any detailed mathematical meaning or purpose for the index of array
> starting from zero and ends in (n-1) instead of starting from 1 and ends in n?
> I only know that the array's name is the address of its start point and it is convenient to set it
> as index 0 and let all the other elements' index as how many offsets it is from the start point.


Other than the obvious 'close to hardware, so convenient, and better performance' that you mentioned, one clear advantage of zero based indexing is that it can be used elegantly in conjunction with modular arithmetic. For instance, to implement a circular buffer over an array of size N,
incrementing a zero-based index i is simple: i = (i+1)%N ;
https://en.wikipedia.org/wiki/Zero-based_numbering#Numerical_properties

Dijkstra argues that even in non-programming scenarios, numbering of indices into a sequence should start at zero; the idea being that it is more elegant to deal with the half-open interval [0,n) rather than the closed interval [1,n].
'Why numbering should start at zero' : https://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EWD831.html
Last edited on
I don't see how it could be done at compile when the array addresses and indices might only be known at runtime.

the only way I know to make that work would be to make the arrays 1 larger than asked for at compile time, so that the +1 indexing works, and the 0th location can't be reached anymore. wasting 1 location in favor of not doing the extra math on each index. I am no compiler writer or expert on that ... but it seems like there would be an efficient approach to the issue for at least the vast majority of cases -- it may have to have a 'do the math' fall back if all else fails.

and I have seen code written that way in c++.
Last edited on
I don't think you actually mean at "compile time", arrays can be created at runtime, but ignoring that I guess it would work but it would complicate things. It seems like a high price to pay just for respecting the mathematical conventions. As common as zero-based indices has become now I would even argue that it's questionable to break programmers' expectations by using one-based indices in new languages.
Is there any detailed mathematical meaning or purpose for the index of array starting from zero
Meaning or purpose, if you are looking for signification with maths you may consider that mathematics is "only" a complementary science for others. Math by itself makes so much sense as for example the solar system on its own.
In addition, 0, nought, le zéro is now well known since several centenaries (leastwise!), you could get used to it.
https://en.wikipedia.org/wiki/0#History
Look at it as "a computer" does, then zero-based arrays are quite natural.
In case you still like to count like postmans do street numbers, just make your array one element too large. This frees the first one (index 0) for other purposes -- didn't you search for purpose? ;)
Topic archived. No new replies allowed.