Why do indices of an array start with 0 and not 1?
The base address of an array is also the address of it's first element. To calculate the address of a random element in an array, the following formula is used:
A = B + P * s
where,
A : Desired address of the element
B : Base address of the array
P : Index of the desired element
s : Size of the data type of element.
If array indices started with 1:
Now, consider the following 1-D array with 5 elements in which the index of first element is 1 and not 0 and we desire to find the address of the 4th element of the array:
To find the address of the fourth element:
Address of 4th element = 400(Base Address of the array) + 4(Index of the element) * 4 (Size of integer data type) So, Address of 4th element = 416 .
But, the real address of the 4th element is 412. Now, let's try to find the same when the starting element of the array is 0:
When array indices start with 0:
Address of 4th element = 400(Base Address of the array) + 3(Index of the element) * 4 (Size of integer data type) So, Address of 4th element = 412. Which is correct.
Hence, the first index of an array is 0 and not 1.