Skip to content

Instantly share code, notes, and snippets.

@jonnyarnold
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonnyarnold/c2bd1782ac48b31cb464 to your computer and use it in GitHub Desktop.
Save jonnyarnold/c2bd1782ac48b31cb464 to your computer and use it in GitHub Desktop.
An introduction to pointers in C

Let's assume we have a 16-bit computer with 4 words of memory (= 8 bytes, really underpowered!) that we can store variables in. Let's label them 01-04 and write out what's in them in binary:

Memory
00: 0000
01: 0000
02: 0000
03: 0000

Next we write some code:

int x = 15;

When this is run, the program will grab some empty memory and fill it with the expected value:

Memory
01: 1111
02: 0000
03: 0000
04: 0000

The program will also keep an address table; this is essentially a dictionary with variable names as keys and memory addresses as values:

Address Table
x: 01

Let's write some more code:

int y = x;

This line says 'get the value in x and store it in y'. As a result, the program does the following:

  • Check the address table. The value of x is stored in memory location 01!
  • Get the value. In this case it is 15.
  • Create a new variable called y, with initial value 15. This fills up memory location 02 with the value 15. This also adds y: 02 to the Address Table. To summarise, we now have:
Memory
01: 1111
02: 1111
03: 0000
04: 0000

Address Table
x: 01
y: 02

Let's go crazy and have a pointer:

int* pointerToX = &x;

So what happens here? This line says "Create a new variable pointerToX. This is a pointer. Fill the variable with the address of x". This is what the program does:

  • Check the address table and get the address of x. Therefore, we get the value 01.
  • Create a new variable pointerToX with initial value 01. This fills up memory location 03 with the value 1. This also adds pointerToX: 03 to the address table. To summarise, we now have:
Memory
01: 1111
02: 1111
03: 0001
04: 0000

Address Table
x: 01
y: 02
pointerToX: 03

Now we have to be careful:

pointerToX;   // = 1  (for memory address 01)
*pointerToX;  // = 15 (the value at address 01)

This is potentially the source of lots of problems. For instance, you can do:

pointerToX = 2;
*pointerToX; // = 15 (this is the value at address 02!!!)

The amount you have to be careful around these things is one of the reasons pointers are now safely tucked away behind high-level programming languages.


So what's the point of pointers? Well, if you have a large binary file or something like that you won't want to have more than one copy kicking around, so you create it once and use pointers whenever you want to refer to it again.

Another important use of pointers is in arrays. Assume you have an array in memory of the Fibbonacci sequence:

Memory
01: 0001
02: 0001
03: 0010
04: 0011
05: 0101
06: 1000
07: 1101

If you had a pointer that started at 01, you could just add one to the pointer every time you wanted the next item! This is known as pointer arithmetic.

Pointer arithmetic is a dangerous practice. The reason why isn't abvious above because I've hidden away the fact that memory is not usually allocated sequentially. Typically, if you have an array at address 01 you're unlikely to get the next element at address 02.

My fingers are now tired. More later.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment