CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Karl Seguin

.NET From Ottawa, Ontario - http://twitter.com/karlseguin/

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;


Comments

Jared Parsons said:

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.  

# May 6, 2008 9:34 PM

Dew Drop - May 7, 2008 | Alvin Ashcraft's Morning Dew said:

Pingback from  Dew Drop - May 7, 2008 | Alvin Ashcraft's Morning Dew

# May 7, 2008 8:28 AM

vkelman said:

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?

# May 7, 2008 2:53 PM

James Curran said:

>> 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.

# May 14, 2008 11:35 AM

Nick said:

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

# May 30, 2008 10:04 AM

DotNetKicks.com said:

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# May 30, 2008 11:24 AM

Code Heaven said:

These are all blog posts I flagged as being particularly interesting, but ones where I may not have anything

# June 6, 2008 10:38 PM

Karl Seguin said:

I tend to subscribe to the belief that programmers with some C background are typically better off than

# August 4, 2008 3:04 PM

Community Blogs said:

I tend to subscribe to the belief that programmers with some C background are typically better off than

# August 4, 2008 4:00 PM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add
Check out Devlicio.us!

Our Sponsors

Free Tech Publications