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:

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 = &amp;myInteger;
//myInteger is passed as a copy to this function and so will
//only be 0 within "WrongSetToZero"
WrongSetToZero(myInteger);
printf("%d\n", myInteger);
//The memory address of myInteger is passed into this function,
//so myInteger will become 0
CorrectSetToZero(pointerToMyInteger);
printf("%d\n", myInteger);
}

The output from this will be:
10
0

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: