+ 1

Why array index start with 0

array index

8th Nov 2016, 6:18 PM
sabahat
sabahat - avatar
3 odpowiedzi
+ 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
8th Nov 2016, 7:54 PM
Abdelaziz Abubaker
Abdelaziz Abubaker - avatar
+ 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.
8th Nov 2016, 10:02 PM
Giulio Pellitta
Giulio Pellitta - avatar
+ 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.
8th Nov 2016, 7:10 PM
Giulio Pellitta
Giulio Pellitta - avatar