+ 6
Why does indexing starts from 0?
7 Respuestas
+ 8
Arrays use up a block of memory locations, and the first element of the array contains the exact memory location of the array, so it's not offset, therefore the first index being 0
+ 6
Arr[n] refers to a memory location that is n-elements away from the starting element.
You can refer to this link : https://stackoverflow.com/questions/16892862/why-array-index-start-from-0
+ 4
in Many OOP languages we could find the array index mainly starting from 0.....the reason being it becomes helpful to locate the elements..... either from back or from front.....
e.g.
arr[5]={3, 7, 2, 8, 9} ;
here arr[0]=3 and
arr[1]=7 and
arr[-1]=9
which would mean the negative index is always the same as number of elements in array....
in the above example...
The indices could range from
[-5, 4].......
+ 4
In some other languages indexing starts at 1.
0
I think it's the same reason computers use binary 0, 1 but not something like say 20, 68.
It's a matter of how easy it lets you work with it. Furthermore, it's about convention. Of course you can start at arbitrary position if you create you own language but it will make your work tough and confuse the users of your language. So 0 is what "they" decided and it comes with a lot of benefits compared to any other number.
0
Remnant from the ol' days.
Back then when you created an array, it would create a block in memory of certain size: if size of your type was 2 bytes and array length was 5, then 10 bytes would be reserved.
When you try to get value from array, it would look at first position in array and then add offset multiplied by type size: in previous array, arr[0] would go to start and stay there, returning value from bytes 1 and 2, while arr[3] would offset it by 3*2=6 bytes and return value from bytes 7 and 8.
Not all languages use arrays like that anymore, but everyone was used to first element being 0 that many (but not all) choose to also make their arrays to start at 0.