Why Array Index Starts With Zero ( 0 ) ?

Hello there, I hope you and your loved ones are doing great in these unprecedented Times of COVID-19.
Have u ever wondered why indexing of Array Data structure starts with 0 but not with 1?? how does the memory get allocated when you declare an Array??
In this blog, I try to cover a basic data structure namely ARRAY's index value i..e, why its index starts from zero (0) but not from one (1)?
Topics Explain in this Blog
1. what's Data Structure(DS)?
2. what's an array?
3. Memory management of array DS.
4. Why indexing starts with 0?
To Explain the 4th point I need to create a base for that which is covered by the 1,2,3 points.
SO, Let's get started.
1. what is Data structure??
A computer program is a collection of instructions to perform a specific task. For this, a computer program may need to store data, retrieve data, and perform computations on the data. so, the data structure is a way of storing and organizing data in an efficient way.
There are various data structure exists namely fews are Arrays, LinkedList, Tree, Graphs ..and many more. Each one has its own specific usage and application.
In this blog, I'll cover array data Structure.
2. what is an Array Data Structure??
An array is a linear type data structure that stores the same type of data, in a contiguous memory location. The idea is to store multiple items of the same type together. This makes it easier to calculate the position of each element by simply adding an offset to a base value, i.e., the memory location of the first element of the array (generally denoted by the name of the array).

An Array data structure is quite Famous and every developer at least is familiar with this type of data structure. knowing completely about it is must for every developer.
We can access the data members of the array by using its index. say if I write arr[0] then it'll give me the first element and so on...
3. Memory management of Array Data structure.
int arr[] = new int[3]; Defining array in java

the int[] arr is just the reference to the array of 3 integers. if you create an array with 10 integers, it is the same – an array is allocated and a reference is returned.

Since Arrays stores data in a contiguous manner, all the data members are in close proximity. So whenever any data member is accessed (Say at index 3, value 89) then its neighbour data members(say data members present at index 2 (value 17), 4(value 1)) are also get stored in the cache. So Array also offers cache friendliness.
4. Why indexing starts with 0?
Let's have an array of the int data type of length 5
int[] arr = {21 , 83 , 39 , 4, 2};
arr is stored in a contiguous memory location in a heap.
Let's say the continuous memory location is from 200 to 220. i..e
21 is stored in heap at a location of 200 address. NOTE: first index position is also known as the base address of an array
83 is stored in heap at a location of 204(200 + 4) address. and so on.....
why 2nd element store 4 unit away from the first?? it's because int data type requires 4 bytes of space to stores its data. if this data type is of char type then it'll require only 1 byte of space. So the addressing is directly dependent on the previous address and the size of the data type.
so the 3rd element will be stored at = address of previous element(204) + 4 = 208.
And now you are so intelligent you can figure out the addresses of the next data members.
we can summarize the above addressing calculation in a formula as:
Array index at ith position = base address of an array + (index value * size of the data type)
so let's say we want to find the address of the 3rd element
3rd pos. index = 200 + 3 * 4 = 212.
so 3rd element is stored at 212 address in the heap memory location, Whenever someone wants to access this then they can directly give the value present at this address.
This is how actually compiler works.
Finally, Now We have made the base for our question I..e why index from 0??
int[] arr = {21 , 83 , 39 , 4, 2};
suppose we start from index 1 in the above array and we have a contiguous memory location from 200 to 220. 21 is present at address 200 83 is present at address 204 and so on...
so the base address(First element address) is at 200..means we can access the first element by using this index.
let us try to calculate this by using the standard formula:
1st position index = base address + index value size of data type(int) = 200 + 1 4 = 204. So by using the formula, it's saying that your 1st element is at index 204, but in reality, at 204 2nd element is present . it's contradicting our existing arrays memory location.
Our assumption to initialize the array from 1 takes a back seat since it's not yielding the correct answer.
That's why the Array index Starts from 0, not from 1.
Thanks for reading. please correct me if I have done anything wrong in explaining. I hope you like my tiny effort in giving back to the community.
You can Connect me on Linkedin:-linkedin.com/in/arshad-al-ali
You can Connect me on Twitter:- https://twitter.com/Arshad63555689



