[Tutor] 'Common' mistake ... for other newbies

Alan Gauld alan.gauld at blueyonder.co.uk
Mon Apr 12 19:59:24 EDT 2004


> Are they here people who think that such a so-called "mistake" is
not a
> mistake ? That David programmed  the way he (and we, humans  should
; and
> that the error lies in language, not in his brain ? Why are indexes
based on
> 0 instead of 1 ?

In some languages they aren't but it has become part of programming
culture.
Partly it comes from the C language. And the reason it adopted it is
that
it used the index as an address offset.
So the first element of the array was located at the list location
itself (zero offset) and the next element was at the list location
plus one, so index one. Some ASCII art follows, it will only work on
fixed width fonts like courier...

List -----> Item1   Address = 12345  => List + 0
     |      Item2   Address = 12346  => List + 1
     |      Item3   Address = 12347  => List + 2
     |      .
     |      .
     +----> ItemN   Address = 123XX  => List + N-1

Thus for any language written in C it is very easy to implement
zero based indexing. And experienced programmers are used to it.
There are also some mathematical reasons for doing this in that
it makes some algorithms easier to code, but I believe the real
reason was just the ease of creating fast indexing based on
address arithmeticin C, and now its traditional.

Alan G
Author of the Learn to Program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld




More information about the Tutor mailing list