Edsger Dijkstra explains why array indexes should start at 0 rather than 1. [pdf]

Here’s the summary: first, let’s consider question of how to represent a range of natural numbers. For example, consider the range of integers . There are four compact ways to represent this by varying whether we use or :

Which is best? In the last two variants, we can subtract the first number from the last to find the size of the range. Based on that favorable property, let’s restrict our options to those two.

What if we instead wish to represent ? Our two candidates give us:

The second representation forces us into the realm of unnatural numbers, which is not ideal. So we have our winning representation: .

Let’s return to the original problem of indexing things. We have two choices for representing the entire range, depending on whether we start at or :

Indexing starting at requires a clumsy ; starting at allows us to write the number of elements directly as part of the range. We conclude that indexing starting at 0 allows us the greatest notational convenience.

William Morgan, July 6, 2011.
This article was labeled as

Comments

To reply to the article, enter your email address. A copy of the article will be sent to you via email.