+ 1
Why does an array index start from zero..
answer me in simple sentence
3 Answers
+ 5
If you're standing in place, are you in a place? Yes. Have you moved in any direction yet? No. So you're in position 0 when standing in place. When you take a step forward, you've moved 1 step, so your index from your original starting point is 1. If you take another step, you're now 2 steps away from your original point, which is a total of 3 points. If you go back 2 steps from there, you're back at your original point, so you're back at 0 in reference to your starting point.
+ 3
The reason is in mathematics:
A variable storing an array, in reality doesn't store the array itself, but the memory address of the array.
Each item of the array need S space of memory to be stored.
First item of the array is stored at the same memory address M of the array itself.
Second item is stored to M+S
Third item is stored to M+2*S...
Nth item is stored to M+(N-1)*S...
So, with indexes starting from zero, the formula to get the memory address of the <index> item is simply:
M + <index>*S
+ 1
Because array refers to the memory location of its elements which starts with zero. The index of the array is an offset.