Monday 30 October 2017

C Programming – Arrays and Pointers

One of the things I’ve discovered, through teaching C programming, is that many students find it extremely hard to figure out how arrays work. In most modern programming languages, arrays seem to be simple lists of items like a virtual filing cabinet with numbered drawers for storing things. But that’s quite a ‘high-level’ abstraction of an array. C doesn’t do arrays like that.

In C, you are forced to deal with the way the computer actually stores these items in memory. And that’s when you discover the surprising fact that an array – or a string – is an address. That’s right. It is a memory location. What? But how can that be…?

Let me explain. The address of the array is the same as the address of the first item in the array. To understand this, let’s look C strings. C does not have a dedicated string data type. In C, a string is an array of characters that are terminated by a null character ‘\0’.

Consider this short program:

#include <stdio.h>

int main(int argc, char **argv) {
char str1[] = "Hello world";
printf("%s %c %d %d %d\n", str1, str1[0], &str1, &str1[0], str1);
return 0;

First it displays string – that is, the array of characters – of the variable str1, then the character at index 0, that is, at the first index of the array which here is H of ‘Hello world’. Then it shows the address of the array, which is this number representing a memory location (here, I am using %d in the format string to show it as a decimal value, but many compilers prefer you to use %p to show a hexadecimal value). Now I get the address of the first character. Remember that I said that the address of the array is the same as the address of the first item in the array. Well, if you run this program you will see that is true because the address of the character ‘H’ (which is shown when I use the ampersand ‘address-of’ operator) is the same as the address of the string, shown when I use the address-of operator with the string variable, str1. But look at this last value here. Instead of using the ampersand to get an address, I just display the variable itself, str1, as an integer using %d in my format string. And this shows the same number – the address of the array.

The address of an array is the same as the address of the first item in the array. Because it’s where the array begins. And the name of the array – that is the name you give to the array variable – is also the address of the array.

In other words, we may tend to think of arrays as fixed-length lists. And in many other programming languages, that may be all you need to know about arrays. But, in fact, an array is really the same as an address in memory that defines the beginning of a list of data items. So when you deal with arrays, including strings, you are dealing with addresses.

The video below is taken from my online course, Advanced C Programming: Pointers.

Save 63% by clicking this link to subscribe to the course for just $35 (regular price is $95):