What arrays and pointers have in common – or – Why arrays start at zero

Every now and again someone asks why do array indexes start at zero rather than one? It’s a good question, because the 1st item in the array is…well..the 1st, not the 0th. Although the answer is easily found on the web, I figured it would tie in nicely with where we left off last time – looking at memory in .NET. Not surprisingly this stems from the good old C days (and perhaps even earlier). You see, back then, arrays and pointers were highly related. I actually remember thinking of them as identical things with different syntax. Once you see that arrays are just syntactical sugar to pointers, you’ll understand why everything starts at zero. I’m not sure how much of this still applies in modern languages. I assume that arrays and pointers are still closely correlated – but I’m pretty sure zero now sticks around for convention. Everything below is in C.

When you create an array, you cut out a continuous chunk of memory  which stores each value in its own block. So:

int values[3] = {1, 20, 30};

will cut out 96bits of memory (3*32) and place the values “1″ in the first, “20″ in the second and “30″ in the third blocks

Since this (like everything else) is just memory, we can assign it to a pointer:

int *p = &values[0]; //the & operator returns the address of a value

The above code assigns the address of the first value to our pointer. So we can say that p points to the start of our array. From our pointer, we can get the 2nd element or 3rd element:

printf("%d\r\n", *(p+1));
printf("%d\r\n", *(p+2));

As you can see, *(p+i) is the exact same thing as saying values[ i ]. So, if you look at it from the point of view of pointers, the zero index makes perfect sense because it signifies the memory offset from the start of the array. To prove how tightly coupled the two concepts are, we can actually change:

int *p = &values[0];

to 

int *p = values;

since values array is nothing more than a pointer to the first element.

While we’re here, let’s look at a crazy example that shows the danger (and power) of C. Even though we’ve only allocated 3 spots for our array, we can easily peak at what’s next with either of these statements:

printf("%d\r\n", *(p+3));
printf("%s\r\n", values[3]);

Worse, we can write an arbitrary value there:

*(p+3)  = 0;
This entry was posted in Uncategorized. Bookmark the permalink. Follow any comments here with the RSS feed for this post.

4 Responses to What arrays and pointers have in common – or – Why arrays start at zero

  1. Nick says:

    Or as my Software Engineering professor used to say… “Zero is a perfectly good number, so why let it go to waste?”

  2. James Curran says:

    >> since values array is nothing more than a pointer to the first element. <<

    I’d write that as “since values array is nothing more than the ADDRESS of the first element.”

    “pointer”, to me at lest, implies “variable”, while “address” implies a fixed value.

  3. vkelman says:

    On the opposite side, in one of modern functional languages, namely Scala, arrays are nothing more than instances of Array class (index still starts from 0, though):
    val greetStrings = new Array[String](3)
    greetStrings(0) = “Hello” // setter method – normal shortcut syntax
    greetStrings.upadete(0) = “Hello” // setter method – equivalent verbose syntax
    println(greetStrings(0)) // getter method – normal shortcut syntax
    println(greetStrings.apply(0) // getter method – equivalent verbose syntax

    Isn’t it exciting?

  4. Even more fun. Since array indexs are just a pointer offset, it’s valid to have a negative offset and index backwards in memory.

    int *p = &values[0];
    p+= 2;
    printf(“%d\n”, p[-1]);

    Not recomended, but it works.