A-level Computing/AQA/Problem Solving, Programming, Operating Systems, Databases and Networking/Programming Concepts/Pointers





A pointer is a data type whose value refers directly to (or "points to") another value stored elsewhere in the computer memory using its address.

Pointers are incredibly important computing concepts as we need pointers to create many of the ADTs (Abstract Data Types) we are going to talk about. Some languages don't allow you to directly manipulate them, but all languages use them. For this section of the book you need to be familiar with the concept of pointers as we'll be using them heavily when we talk about linked lists, queues and stacks.

Quick examples of pointers might include the next free memory location in an array, or the location in memory that an array is stored at.

Let's take a look at how pointers are used in the C programming Language.

We are going to deal with two variables: a ptr We are also going to meet two key characters: - where  would return the address location of variable - where  means follow the memory address stored in ptr and set that location to 8

Assume that  is located at address 0x8130 in memory and   at 0x8134; also assume this is a 32-bit machine such that an int is 32-bits in size. The following is what would be in memory after the following code snippet is executed:


 * {| class="wikitable"

! Address !! Contents
 * 0x8130 || 0x00000005
 * 0x8134 || 0x00000000
 * }
 * 0x8134 || 0x00000000
 * }

(The NULL pointer shown here is 0x00000000.) By assigning the address of  to  :

yields the following memory values:


 * {| class="wikitable"

! Address !! Contents
 * 0x8130 || 0x00000005
 * 0x8134 || 0x00008130
 * }
 * 0x8134 || 0x00008130
 * }

Then by dereferencing  by coding:

the computer will take the contents of  (which is 0x8130), 'locate' that address, and assign 8 to that location yielding the following memory:


 * {| class="wikitable"

! Address !! Contents
 * 0x8130 || 0x00000008
 * 0x8134 || 0x00008130
 * }
 * 0x8134 || 0x00008130
 * }

Clearly, accessing  will yield the value of 8 because the previous instruction modified the contents of   by way of the pointer.

What would the following output, where  writes a line to the screen: