

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Difficulty: 8
click for more info
Not enough gems
Cost: 6 gems
1: Pointer-Pointers
incomplete
2: Array of Pointers
incomplete
3: Void Pointers
incomplete
4: Swapping Integers
incomplete
5: Swapping Strings
incomplete
6: Generic Swap
incomplete
This lesson's interactive features are locked, please to keep using them
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.
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.