We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Pointer-Pointers

Ok, so... now we know what pointers are. So let's learn it over again! A pointer-to-pointer in C is just a pointer variable that holds the address of another pointer.

Click to play video

This allows you to create complex data structures like arrays of pointers, and to modify pointers indirectly. The syntax is exactly what you would expect:

int value;
int *pointer;
int **pointer_pointer;

Pointers to pointers (or pointers to pointers to pointers to pointers... you get the idea) are like a treasure map or a scavenger hunt. You start at one pointer and keep following the chain of addresses until you get to the final value. Each * follows one address: *pointer_pointer gets the inner int *, and **pointer_pointer gets the final int.

Interactive example available with JavaScript enabled.

Assignment

Complete the allocate_int function. It accepts a pointer to a pointer to an integer called pointer_pointer, and a raw value. Update the pointer that pointer_pointer points to so it stores the address of newly allocated memory containing value.

Observe: If you take a look at test_does_not_overwrite in the main.c file, you'll notice that while pointer_pointer does indeed unravel and point to the new value, the original value still exists at its spot in memory. We just don't point to it anymore.