0

Why array should start from 0 ?

22nd Oct 2017, 6:53 AM
Prajakta Madhukar Varute
Prajakta Madhukar Varute - avatar
9 Answers
+ 2
memory allocation start from 0
22nd Oct 2017, 7:11 AM
vishwanath rao
vishwanath rao - avatar
+ 4
int a[] = {3,2,4}; printf("%i",*(a+0)); //Out 3 //**** a[0] == *(a+0)
22nd Oct 2017, 7:11 AM
Yanothai Chaitawat
Yanothai Chaitawat - avatar
+ 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.
22nd Oct 2017, 7:27 AM
John Wells
John Wells - avatar
+ 1
ok thank you very much all of you guys ā˜ŗļø
22nd Oct 2017, 7:32 AM
Prajakta Madhukar Varute
Prajakta Madhukar Varute - avatar
+ 1
because index is the distance from 1st character this makes it easier to locate them.
22nd Oct 2017, 8:30 AM
shobhit
shobhit - avatar
+ 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]
24th Oct 2017, 7:04 AM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
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 ?
22nd Oct 2017, 7:14 AM
Prajakta Madhukar Varute
Prajakta Madhukar Varute - avatar