Why array is not starting from 1 instead of 0

Words
232
Reading
2 min
Listen
Play
9y

Some programming languages (BASIC, for example) did exactly that.

But zero is a more natural starting point for programmers than one. Zero is the first unsigned integer - so why start with the second one?

The other reason is that arrays and pointers are natural cousins - so when you write to the location pointed at by a pointer variable, you’re effectively writing into the first element of an array starting at that location. Adding a number to a pointer works similarly to indexing an array.

In this code:

int array [ 10 ] ;
int *pointer = array ;

array [ 3 ] = 1234 ;
pointer [ 3 ] = 1234 ;
*(pointer+3) = 1234 ;

…with arrays that start at zero, lines 4, 5 and 6 do the exact same thing.

…but with arrays that start at one, line 4 and line 6 would clearly mean something entirely different from each other - which would make line 5 seem…well…kinda odd! If line 5 did the same thing as line 4, the compiler would have to subtract 1 from the index in line 5 - and line 6 would have to have a 2 in it instead of a 3.

So zero-based arrays actually allow pointers and arrays to be used harmoniously - as they are in C - and that would be difficult and/or ugly if arrays started at ‘1’.

Why array is not starting from 1 instead of 0 | Ecency