Why array index starts from zero ?

Prerequisite : Pointers in C/C++

Consider int arr[100]. The answer lies in the fact how the compiler interprets arr[i] ( 0<=i<100).
arr[i] is interpreted as *(arr + i). Now, arr is the address of array or address of 0th index element of array. So, address of next element in array is arr + 1 (because elements in array are stored in consecutive memory locations), further address of next location is arr + 2 and so on . Going with above arguments, arr + i means address at i distance away from starting element of array. Therefore, going by this definition, i will be zero for starting element of array because starting element is at 0 distance away from starting element of array. To fit this definition of arr[i], indexing of array starts from 0.





using namespace std;
int main()
    int arr[] = {1, 2, 3, 4};
    // Below two statements mean same thing
    cout << *(arr + 1) << " ";
    cout << arr[1] << " ";
    return 0; 



2 2

The conclusion is, we need random access in array. To provide random access, compilers use pointer arithmetic to reach i-th element.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.