Sunday 6 September 2015

Indexing in array starts with 0


Why does the index in array starts with 0 and not 1 ?

In C name of an array is a pointer, referencing a memory location. The expression arr[n] refers to memory location n elements away from starting element. This means that index is used as an offset. So if we want to access arr[1] it would be one element away from starting element and that would waste one cell or to be more precise, it would waste memory equal to memory used by one element. So, the first index is always 0 (0 elements away).

The decision to start count at 0 , has since spread throughout 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. Concluding, it makes compiler translation easier.





Why does the index in array starts with 0 and not 1 ?
To see more such questions click the link below:More Questions

No comments:

Post a Comment