0
Why array should start from 0 ?
7 Réponses
+ 2
memory allocation start from 0
+ 4
int a[] = {3,2,4};
printf("%i",*(a+0));
//Out 3
//****
a[0] == *(a+0)
+ 2
It was picked because it simplified the compiler's generated code. Given the following C++ code:
char b[2];
int i=1;
b[i]='a';
The compiler would generate code to take the address of b and add the value of i to it, giving the address to store the 'a'. If the array started at some other number, you would have to subtract it out to become zero based. Bigger structures are handled by multiplying i by the size before doing the addition. The original C compiler had to fit in less than 64K so a lot of the language simplicity was forced by that limit. C++ maintained backward support for C and added the world of OOP.
+ 1
ok thank you very much all of you guys ☺️
+ 1
because index is the distance from 1st character this makes it easier to locate them.
+ 1
To explain the real meaning, you need to know something :
v[a] is replaced by *(v + a)
v is the address of the starting point of the contiguous space allocated, so to obtain the first element, you need to do :
*v equivalent to *(v + 0) equivalent to v[0]
0
http://developeronline.blogspot.in/2008/04/why-array-index-should-start-from-0.html?m=1
I found one reason on this blog ,so is there any other reason ?