Visualizing pointer chains
What you'll learn: How to mentally map multiple levels of redirection by treating pointers as a chain of treasure maps.
The Treasure Map Analogy
A standard pointer is like a treasure map: it doesn't contain the gold itself, but it tells you exactly where to find it. A pointer to a pointer (declared as int **ptr) is like a locked box that contains a second treasure map.
To get to the actual value, you have to follow a chain:
- You look at the first map (the pointer to a pointer).
- It leads you to a second map (the pointer).
- That second map finally leads you to the treasure (the actual integer).
In C, each asterisk (*) represents one "hop" you must take to reach the data.
Seeing the Chain in Code
When we see int **ptr, we are looking at a variable that stores the memory address of another pointer. Here is how that chain looks in practice:
#include <stdio.h>
int main() {
int treasure = 500; // The actual data
int *map = &treasure; // A pointer to the data
int **box = ↦ // A pointer to the pointer
// Level 0: The box itself (an address)
printf("Box address: %p\n", (void*)box);
// Level 1: One hop takes us to the map
printf("Map address: %p\n", (void*)*box);
// Level 2: Two hops take us to the treasure
printf("Treasure value: %d\n", **box);
return 0;
}
Why the Chain Matters
Visualizing this as a chain helps you understand how memory is linked. If you change where *box points, you are effectively swapping the map inside the box. If you change **box, you are reaching all the way through the chain to modify the original treasure.
This "chaining" is the foundation for complex data structures. When you see ***ptr, don't panic—just imagine three maps leading to one another. Each * is simply a step you take along the path until you reach the final destination.
Key takeaway
A pointer to a pointer is just a link in a chain, where each * operator follows one link to the next memory address.