java - Why doesn't the index/list of an array begin with 1? -
this question has answer here:
- why indexing start 0 in 'c'? 12 answers
is there special reason? know that's how language has been written, can't change it? , challenges we'd face if index start 1?
for historical reasons, , reasons connected how array "made" in memory.
in c, array piece of memory (with information @ compiler level on size). have pointer (a reference) first element. go second element can do
int array[10]; // array int *p = array; // reference first element of array int *q = p + 1; // reference second element of array int *r = p + 2; // reference third element of array
clearly, symmetry:
array[0] // reference first element of array array[1] // reference second element of array array[2] // reference third element of array
the [x]
operator of c in fact compiled array + x
.
you see? array "base 0" in c. , reason in many other languages it's same.
now, c++ has roots in c, java has roots in c++ , other languages, c# has roots in c++, java , other languages... same tree.
basic tree :-)
Comments
Post a Comment