Pointer to a Pointer
A pointer that points to another pointer.
4 minute read
Big idea
A pointer is a variable too — so it has its own address, and another pointer can point to it. That's a pointer to a pointer: int **.
The idea
int x = 10;
int *p = &x; // p holds x's address
int **pp = &p; // pp holds p's address
Each * follows one arrow:
| Expression | Means | Value |
|---|---|---|
pp | the address of p | 0x…08 |
*pp | what pp points at: p | the address of x |
**pp | what p points at: x | 10 |
Why would you want one?
The same reason as any pointer: to let a function change something that belongs to the caller. If the thing you want to change is itself a pointer, the function needs a pointer to that pointer.
#include <stdio.h>
void point_at_bigger(int **target, int *a, int *b) {
if (*a > *b) {
*target = a;
} else {
*target = b;
}
}
int main(void) {
int left = 3, right = 8;
int *best = NULL;
point_at_bigger(&best, &left, &right);
printf("best points at %d\n", *best);
return 0;
}output
best points at 8Run and edit it in the app.
You'll meet ** again with arrays of strings — main can even take char **argv, a list of the words typed on the command line.
Watch it
Think of it like
A treasure map that leads to a second map, which leads to the treasure. Two hops, but you still end up at the same chest.
🕷️ Spidey-sense: A note in my locker that says which rooftop has my backpack. The locker note → the backpack location → the backpack.
**is just "follow it twice".
Key terms
- Pointer to a pointer — A variable holding the address of a pointer:
int **pp. - Level of indirection — How many hops it takes to reach the value; each
*is one hop.
Check yourself
What type holds the address of an int *?
x = 3; p = &x; pp = &p; What is **pp?
Why pass an int ** to a function?