Why does the indexing in an array start with zero in C and not with 1?
问题:
回答1:
In C, the name of an array is essentially a pointer, a reference to a memory location, and so the expression array[n] refers to a memory location n-elements away from the starting element. This means that the index is used as an offset. The first element of the array is exactly contained in the memory location that array refers (0 elements away), so it should be denoted as array[0].
for more info:
http://developeronline.blogspot.com/2008/04/why-array-index-should-start-from-0.html
回答2:
This question was posted over a year ago, but here goes...
About the above reasons
While Dijkstra\'s article (previously referenced in a now-deleted answer) makes sense from a mathematical perspective, it isn\'t as relevant when it comes to programming.
The decision taken by the language specification & compiler-designers is based on the decision made by computer system-designers to start count at 0.
The probable reason
Quoting from a Plea for Peace by Danny Cohen.
- IEEE Link
- IEN-137
For any base b, the first b^N non-negative integers are represented by exactly N digits (including leading zeros) only if numbering starts at 0.
This can be tested quite easily. In base-2, take 2^3 = 8
The 8th number is:
- 8 (binary: 1000) if we start count at 1
- 7 (binary: 111) if we start count at 0
111
can be represented using 3
bits, while 1000
will require an extra bit (4 bits).
Why is this relevant
Computer memory addresses have 2^N
cells addressed by N
bits. Now if we start counting at 1, 2^N
cells would need N+1
address lines. The extra-bit is needed to access exactly 1 address. (1000
in the above case.). Another way to solve it would be to leave the last address inaccessible, and use N
address lines.
Both are sub-optimal solutions, compared to starting count at 0, which would keep all addresses accessible, using exactly N
address lines!
Conclusion
The decision to start count at 0
, has since permeated all digital systems, including the software running on them, because it makes it simpler for the code to translate to what the underlying system can interpret. If it weren\'t so, there would be one unnecessary translation operation between the machine and programmer, for every array access. It makes compilation easier.
Quoting from the paper:
回答3:
Because 0 is how far from the pointer to the head of the array to the array\'s first element.
Consider:
int foo[5] = {1,2,3,4,5};
To access 0 we do:
foo[0]
But foo decomposes to a pointer, and the above access has analogous pointer arithmetic way of accessing it
*(foo + 0)
These days pointer arithmetic isn\'t used as frequently. Way back when though, it was a convenient way to take an address and move X \"ints\" away from that starting point. Of course if you wanted to just stay where you are, you just add 0!
回答4:
Because 0-based index allows...
array[index]
...to be implemented as...
*(array + index)
If index were 1-based, compiler would need to generate: *(array + index - 1)
, and this \"-1\" would hurt the performance.
回答5:
Because it made the compiler and linker simpler (easier to write).
Reference:
\"...Referencing memory by an address and an offset is represented directly in hardware on virtually all computer architectures, so this design detail in C makes compilation easier\"
and
\"...this makes for a simpler implementation...\"
回答6:
For the same reason that, when it\'s Wednesday and somebody asks you how many days til Wednesday, you say 0 rather than 1, and that when it\'s Wednesday and somebody asks you how many days until Thursday, you say 1 rather than 2.
回答7:
Array index always starts with zero.Let assume base address is 2000. Now arr[i] = *(arr+i)
. Now if i= 0
, this means *(2000+0
)is equal to base address or address of first element in array. this index is treated as offset, so bydeafault index starts from zero.
回答8:
The most elegant explanation I\'ve read for zero-based numbering is an observation that values aren\'t stored at the marked places on the number line, but rather in the spaces between them. The first item is stored between zero and one, the next between one and two, etc. The Nth item is stored between N-1 and N. A range of items may be described using the numbers on either side. Individual items are by convention described using the numbers below it. If one is given a range (X,Y), identifying individual numbers using the number below means that one can identify the first item without using any arithmetic (it\'s item X) but one must subtract one from Y to identify the last item (Y-1). Identifying items using the number above would make it easier to identify the last item in a range (it would be item Y), but harder to identify the first (X+1).
Although it wouldn\'t be horrible to identify items based upon the number above them, defining the first item in the range (X,Y) as being the one above X generally works out more nicely than defining it as the one below (X+1).
回答9:
The technical reason might derive from the fact that the pointer to a memory location of an array is the contents of the first element of the array. If you declare the pointer with an index of one, programs would normally add that value of one to the pointer to access the content which is not what you want, of course.
回答10:
Try to access a pixel screen using X,Y coordinates on a 1-based matrix. The formula is utterly complex. Why is complex? Because you end up converting the X,Y coords into one number, the offset. Why you need to convert X,Y to an offset? Because that\'s how memory is organized inside computers, as a continuous stream of memory cells (arrays). How computers deals with array cells? Using offsets (displacements from the first cell, a zero-based indexing model).
So at some point in the code you need (or the compiler needs) to convert the 1-base formula to a 0-based formula because that\'s how computers deal with memory.
回答11:
Suppose we want to create an array of size 5
int array[5] = [2,3,5,9,8]
let the 1st element of the array is pointed at location 100
and let we consider the indexing starts from 1 not from 0.
now we have to find the location of the 1st element with the help of index
(remember the location of 1st element is 100)
since the size of an integer is 4-bit
therefore --> considering index 1 the position would be
size of index(1) * size of integer(4) = 4
so the actual position it will show us is
100 + 4 = 104
which is not true because the initial location was at 100.
it should be pointing to 100 not at 104
this is wrong
now suppose we have taken the indexing from 0
then
position of 1st element should be
size of index(0) * size of integer(4) = 0
therefore -->
location of 1st element is 100 + 0 = 100
and that was the actual location of the element
this is why indexing starts at 0;
I hope it will clear your point.
回答12:
Array name is a constant pointer pointing to the base address.When you use arr[i] the compiler manipulates it as *(arr+i).Since int range is -128 to 127,the compiler thinks that -128 to -1 are negative numbers and 0 to 128 are positive numbers.So array index always starts with zero.
回答13:
becoz when we access the array elements following formula is used by compiler ((base address)+index*size) fisrt element always get stored at base address in arrays... So if we start with 1 we cant accesss first element as it gives address of sesond element... so it starts with 0.