29/180: Why Array index starts from zero?

Navneet Ojha
2 min readJul 16, 2021

--

A[i] in array is implemented as *(A+i) , For example if you have an array of integers and each integer takes 4 bytes.
Lets say starting memory address is 100 then memory locations for each element would be

100,104,108,112,116,……

But If Array index starts with 1 then A[i] will be implemented as *(A+i-1) which will be time consuming during compilation and the performance of the program will also be effected. So, it is better to start index of the array from 0.

Array index always starts with zero. Let assume base address is 2000. Now arr[i] = *(arr+i). Now if i= 0, this means *(2000+0)is equal to base address or address of first element in array. this index is treated as offset, so by default index starts from zero.

For the same reason that, when it’s Wednesday and somebody asks you how many days till Wednesday, you say 0 rather than 1, and that when it’s Wednesday and somebody asks you how many days until Thursday, you say 1 rather than 2.

Let me explain this in more brief

Suppose we want to create an array of size 5
int array[5] = [1,2,3,4,5]

let the 1st element of the array is pointed at location 3000 and let we consider the indexing starts from 1 and not from 0. Now we have to find the location of the 1st element with the help of index (remember the location of 1st element is 3000). Since the size of an integer is 4-bit therefore → considering index 1 the position would be size of index(1) * size of integer(4) = 4 so the actual position it will show us is

3000 + 4 = 3004

which is not true because the initial location was at 3000. It should be pointing to 3000 not at 3004. This is wrong, now suppose we have taken the indexing from 0 then position of 1st element should be size of index(0) * size of integer(4) = 0 therefore →
location of 1st element is 3000 + 0 = 3000

That was the actual location of the element this is why indexing starts at 0;

I hope now it would clear with all those examples. If any confusion do comment down.

--

--

Navneet Ojha
Navneet Ojha

Written by Navneet Ojha

I am Indian by birth, Punjabi by destiny. Humanity is my religion. Love to eat, travel, read books and my million dreams keep me alive.

No responses yet