Why does an array index start at 0?


#22

I would guess that a language which allows negative or ad-hoc indices perhaps doesn’t implement an array the same way that, for example, C does – as a contiguous block of memory. Case in point, Lua’s arrays are actually map-like data structures.


#23

C arrays are a very basic (like most things in C) they’re just reserving a block of stack memory. In C if you want to insert into, or resize a C array it becomes expensive as data needs to be copied. The result is that if a language isn’t as low level as C then chances are that it will have an array type based on at least a linked list (if not a tree) to allow quick insertion/expansion etc.


#24

Using zero as the start of a numbering system avoids the corner cases at the boundaries. When applied to programming languages and APIs, treating zero as a first-class citizen reduces the frequency of boundary condition bugs introduced by the humans when writing non-trivial code.

Consider a multi-story building with an elevator. You are going to an appointment on the fifth floor. You walk in from the street and come to an elevator and a staircase. After the appointment, you must fetch something from the second basement of the building.

  1. If you take the stairs, how many floors must you climb?
  2. If you take the elevator, how many times will the elevator chime before you reach your appointment?
  3. After the appointment, how many floors must you descend to fetch the item?
  • In countries like America, you were already on floor 1 when you entered the building. Therefore:
    1. Answer is 4. (Floor 5 - ground floor = 5 - 1 = 4.)
    2. Answer is 4.
    3. Answer is 6. (Floor 5 - basement 2 = 6. Huh?)
  • In other countries, mainly European countries and countries with an Arabic influence such as Spain, you were on floor 0 when you entered the building. Therefore:
    1. Answer is 5. (5 - 0 = 5)
    2. Answer is 5.
    3. Answer is 7. (5 - -2 = 5 + 2 = 7)

Notice that in the “starts at 1” system, you have to deal with some corner cases that just don’t exist in in the “starts at 0” system.

Which system you prefer depends on how comfortable you are with the concept of zero. Is zero a real number to you, or is zero a special case — an “almost number”?

Cultures that were influenced earlier by the spread of the Hindu-Arabic introduction of zero had no problem numbering floors starting at zero.

In my opinion, zero-based systems are superior. Zero isn’t just a nickname for empty container. Zero is a valid location, such as the equator having a latitude of 0. Including zero as a location avoids bugs introduced by incorrectly implementing the resulting special cases. This justifies whatever emotional issues we might have with treating zero as a location.


#25

Yes, I was thinking maybe it has something to do with (0,0) being valid Cartesian co-ordinates in a graph.

As an aside, I would highly recommend the brilliant book Zero: the Nothing That Is, which is a history of zero and explains why it’s crucial to advanced maths.


#26