Honestly
speaking, the doubt is not mine. I rarely think about C language so much that I
get such genuine doubts. Its a friend's doubt. The list of answers I got goes
here.
In programming languages like C the array name is essentially a pointer, a reference to a memory location, so the expression a[n] refers to a memory location away from n elements in the array. The first element in the array is exactly contained in the memory location that the array refers (0 elements away) so it should be denoted as a[0].
Another mathematical solution I found out really took me nowhere. So instead of typing my interpretation, I'm copy-pasting it here, so if anyone understood what it means, enlighten me.
In programming languages like C the array name is essentially a pointer, a reference to a memory location, so the expression a[n] refers to a memory location away from n elements in the array. The first element in the array is exactly contained in the memory location that the array refers (0 elements away) so it should be denoted as a[0].
Another mathematical solution I found out really took me nowhere. So instead of typing my interpretation, I'm copy-pasting it here, so if anyone understood what it means, enlighten me.
"Dijkstra explains why we should index from
0. This is a problem on how to denote a subsequence of natural numbers,
say for example 1,2,3,...,10. We have four solutions available:
a. 0<i<11
b. 1<=i<11c. 0<i<=10d. 1<=i<=10Dijkstra argues that the proper notation should be able to denote naturally the two following cases:
1. The subsequence includes the smallest natural number, 0
2. The subsequence is empty
Requirement 1. leaves out a. and c. since they would have the form -1<i which uses a number not lying in the natural number set (Dijkstra says this is ugly). So we are left withb. and d. Now requirement 2. leaves out d. since for a set including 0 that is shrunk to the empty one, d. takes the form 0<=i<=-1, which is a little...well, messed up! Subtracting the ranges in b. we also get the sequence length, which is another plus. Hence we are left with b. which is by far the most widely used notation in programming now."
a. 0<i<11
b. 1<=i<11c. 0<i<=10d. 1<=i<=10Dijkstra argues that the proper notation should be able to denote naturally the two following cases:
1. The subsequence includes the smallest natural number, 0
2. The subsequence is empty
Requirement 1. leaves out a. and c. since they would have the form -1<i which uses a number not lying in the natural number set (Dijkstra says this is ugly). So we are left withb. and d. Now requirement 2. leaves out d. since for a set including 0 that is shrunk to the empty one, d. takes the form 0<=i<=-1, which is a little...well, messed up! Subtracting the ranges in b. we also get the sequence length, which is another plus. Hence we are left with b. which is by far the most widely used notation in programming now."
There's another explanation, by convention. It's easier to count the numbers from zero it says. Even the eight bits in a computer start from zero.
There are many other explanations and may be all of them are correct, but only these three interested me.
The first one seems most correct. the second one is correct may be because a mathematician explained it. :-) Well, so that's why the array index starts with zero, if you can find only one reason. ;-)
No comments:
Post a Comment
I LOVE COMMENTS! :)