Tutorial 9 – Pointers

There are many different data types in C/C++, and by using structs and classes you can create an endless variety of new ones. One of the more interesting (and sometimes useful!) of the built in types is the pointer.

Put simply a pointer is a reference to a memory address. Let’s see what this means.

int myVariable = 7;

The diagram below is an example of how this code snippet might look in your computer’s memory.

Now what about if we have a pointer:

 int myVariable = 7;
 int *pointerToMyVariable = &myVariable;

This means that you have a variable of type “int pointer” called pointerToMyVariable which equals the “address of myVariable” and it might look something like this in memory:
So why might it be useful to put a load of memory references in your memory?!

Pointers As References

One of the first reasons that comes to mind is related to how variables are passed to functions. All variables that you pass to functions are local copies. This means that any changes made to a parameter do not affect that variable outside of the function. Here’s an example to illustrate:

void WrongSetToZero(int parameter)
    parameter = 0;

void CorrectSetToZero(int *parameter)
    *parameter = 0;

Above are 2 functions, both attempt to set a variable to 0. The first one attempts to change the value of a parameter which is passed in as a copy. Within “WrongSetToZero”, the variable “parameter” will be set to 0 but this will only be the local copy of that variable.
The second function does not attempt to change the local copied variable, instead it “dereferences” it with the ‘*’ character. This equates to following the pointer to it’s memory reference. It then sets the variable in this memory address to 0. If you run it you’ll see the difference!

int main(int argc, char *argv[])
    int myInteger = 10;
    int *pointerToMyInteger = &myInteger;

    //myInteger is passed as a copy to this function and so will
    //only be 0 within "WrongSetToZero"
    printf("%d\n", myInteger);

    //The memory address of myInteger is passed into this function,
    //so myInteger will become 0
    printf("%d\n", myInteger);

The output from this will be:

Pointers As Arrays

Another use of pointers is as an array. Think of what an array is; A contiguous list of variables within memory. If we have access to this section of memory, we can point to the start of it, that pointer is then the start of the array! In order to be able to access sections of memory in this fashion we need to make use of the function “malloc”:

int main()
    int *myArray = (int *) malloc(sizeof(int) * 5);

This might look a bit daunting at first so lets break it down. We know that “int *myArray” means create an integer pointer called myArray. malloc returns a void pointer so we cast it to an int pointer. malloc is short for memory alloc and takes a value representing how much memory is to be allocated. In our case we have told it to allocate enough space to store 5 integers. So what this line does is get a pointer to a block of memory that is the size of 5 integers. Now we can address these 5 memory locations in a couple of ways:

//As an array
myArray[0] = 1;
myArray[1] = 15;
myArray[2] = 72;

//As a memory address plus an offset
*myArray = 10;
*(myArray + 3) = 90;
*(myArray + 4) = 26;

After this we’ll end up with an array containing the following values:
10, 15, 72, 90, 26

The uses of pointers are many and varied. My advice is to experiment with them and see what you can achieve!

  1. No comments yet.
  1. No trackbacks yet.

Tell me what you think!

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: