Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

zero index makes sense because (and i will use C syntax here)

a[b]

is equivalent to

*(a + b)

with a being a pointer and b an integer. this can be seen as well in the fact that

a[b]

is equivalent to

b[a]

where in both cases a is a pointer and b is an integer.

Thus, the index into an array is fundamentally the same as an offset into that array. Usually, in languages that do use 1-based indexing (like in pascal strings), the 0th byte is used for something else.

The point being that thinking in terms of 1-based indices means that you have to use a system where addr+0 is invalid, and the first element of an array at address X is X+1.

Now you could just shift everything by 1 and just declare that +1 means +0, but then you lose all meaning to the offset vs index and ahhhhhh



In my view, the fact that a[b] is equivalent to *(a + b) in C and C++ (in most languages with arrays the latter notation is meaningless) is a sort of a neat coincidence which we shouldn't feel bound to in language design in general.

There are many reasons to depart from this idea in languages far removed pointer arithmetic. For example, Julia has 1-based indexing and I appreciate it for that when doing computational mathematics. It is closer to familiar notation: A[1][1] is the element of matrix A in row 1 and column 1. In a language with zero-based indexing, it is sometimes easier for me to allocate an extra row and column, wasting memory, than to shift indices and risk an inevitable off-by-one somewhere.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: