+ 6
Why does the index of an array starts from 0
Why does the index of an array starts from 0, why not from 1?if it started from 1 it would have been more easier to write codes related to arrays, right?
4 Respostas
+ 8
In C, an array is just a pointer pointing to the first element of the array. You can imagine it like an arrow.
Now, how much do you need to add to get from the first element to the first element? 0.
All the other laguages copied this behaviour from C (and BCPL before it).
In C terms:
a[0] == *(a + 0) == *(0 + a) == 0[a]
They all mean the same.
There are some languages that start from 1 (lua, matlab, fortran, ...), but 0-indexed arrays are not easier or harder than 1-indexed ones. You just need to get used to them :)
+ 4
Treat the index of an array like the offset of the original location, first element is 0 offset from original address, thus index is start from 0.
+ 1
Remember that C is quite a low level language
And it's oriented for computer not for mathematics
Index are number, number are stored in binary form
If index start at 1 you lose 1 bit for nothing, why do that?
0
غيملمخنلبغاننبثقعز