+ 1
Why array index start with 0
array index
3 ответов
+ 5
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]. Most programming languages have been designed this way, so indexing from 0 is pretty much inherent to the language
+ 4
TechTro is correct. Futhermore, in C/C++ you can also use the so-called pointer arithmetic. If an array a starts at some memory address x, its n-th element starts at position x+n*s, where s is the size in bytes of a single element of the array, a number you can get with the sizeof() function. Knowing that you can get/set directly an element of the array.
You can do something like that even for multi-dimensional arrays, but it gets slightly more complex.
+ 3
Why not? If your index can be any non-negative integer it is just a waste of address space not to start with 0. Seriously, pick just about any programming language (the only exception that comes to mind is Matlab/Octave) and array start with index 0 rather than 1.