+ 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?

21st Jan 2019, 1:57 AM
Vikas Tiwari
Vikas Tiwari - avatar
4 ответов
+ 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 :)
21st Jan 2019, 2:09 AM
Schindlabua
Schindlabua - avatar
+ 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.
21st Jan 2019, 3:20 AM
Calviղ
Calviղ - avatar
+ 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?
21st Jan 2019, 10:56 PM
Abel Louis
Abel Louis - avatar
0
غيملمخنلبغاننبثقعز
9th Apr 2019, 7:30 AM
محمد طه طه محمد
محمد طه طه محمد - avatar