Why are Arrays Zero-Indexed?
A common question the comes up when people first begin learning how to program is why do array indices begin with 0? Wouldn’t it be easier and more intuitive to begin at 1? Doesn’t it make more sense to have the last item of an array be equal to the array’s length instead of the array’s length — 1? As we’ll see in this post, it has to do with the way the array items are stored in memory.
So to begin with, in languages such as Java and C#, an array variable actually holds an address in memory. That address is the location of the first item of the array (this isn’t entirely true since in both languages arrays are objects that store other data such as the array’s length and this data comes before the first array items in memory. However, in C/C++ the beginning of the array is the first item).
Since the array is of a certain type, the items are of uniform length and therefore each item of the array can be found as a certain number of data type size offsets away from the first item. For an array of integers intArray, the variable intArray points to the beginning of the array:
If the size in an integer is n, then the location of the next item in the array is at intArray + n.
This can be demonstrated in code. This example is in C#:
class Program
{
unsafe static void Main(string[] args)
{
int[] intArray = { 0, 1, 2, 3, 4, 5 };
fixed (int* p1 = intArray)
{
System.Console.WriteLine("Pointer p1 is pointing to value {0} and is " + "at memory address {1}", *p1, (long)p1);
int* p2 = p1 + 1; System.Console.WriteLine("Pointer p2 is pointing to value {0} and is " + "at memory address {1}", *p2, (long)p2);
}
}
}
This code outputs:
Pointer p1 is pointing to value 0 and is at memory address 74196208
Pointer p2 is pointing to value 1 and is at memory address 74196212
Press any key to continue . . .
In this example the pointer p1 is referencing the beginning of the array. In the output we can see the array value and its location in memory. We then create a new pointer p2 and assign to it the value of p1 +1. We can see that p2 points to the next item in the array and the memory address is 4 bytes away from the first. This is because when 1 is added the pointer, it is onedata type length not one byte. By adding the data-type length to p1, we offset the memory address by that length and we are now at the same address as intArray[1]. This is exactly what the array index is doing.
To sum it up, arrays are zero-indexed because the index of each item represents an offset from the first item in the array.