Pointer to Pointer
What is a pointer to pointer?
You know a pointer holds the address of a variable. A pointer to pointer (or double pointer) simply takes that one step further: it holds the address of another pointer.
So there are three things in play: the value, a pointer to that value, and a pointer to that pointer. It sounds abstract, but a quick picture makes it click.
A picture of the three levels
pp ----> p ----> x (double) (pointer) (value = 10) pp holds the address of p p holds the address of x x holds the value 10 *pp = p (the inner pointer) **pp = 10 (the final value)
Each arrow is one level of "points to". To get from pp all the way to the value, you follow two arrows — which in code means two *.
Declaring and using **
#include <stdio.h>
int main() {
int x = 10;
int *p = &x; // p points to x
int **pp = &p; // pp points to p
printf("x = %d\n", x);
printf("*p = %d\n", *p); // 10
printf("**pp = %d\n", **pp); // 10
return 0;
}x = 10
*p = 10
**pp = 10
Notice **pp reaches the same 10 as *p and x — it just travels through one more level to get there.
Changing a pointer inside a function
This is the real reason double pointers matter. Because C passes by value, a function that takes a single pointer gets a copy and cannot change the caller's pointer. Passing the pointer's address (a double pointer) fixes that.
#include <stdio.h>
#include <stdlib.h>
void allocate(int **pp) {
*pp = malloc(sizeof(int)); // change the caller's pointer
**pp = 99; // set the value
}
int main() {
int *p = NULL;
allocate(&p); // pass address of p
printf("p points to %d\n", *p);
free(p);
return 0;
}p points to 99
Without the double pointer, p would stay NULL back in main. (Compare with call by value vs reference.)
Where double pointers are used
- Functions that allocate memory for the caller and update their pointer.
- Arrays of strings —
char **argvinmainis exactly this. - Dynamic 2D arrays — an array of pointers, each pointing to a row.
- Linked structures — updating the head pointer of a list.
Common mistakes
- Dereferencing the wrong number of times — mixing up
*ppand**pp. - Passing a pointer instead of the address of a pointer when a function needs to change it.
- Forgetting that
**ppis only valid if both levels point somewhere real. - Getting lost in extra levels — more than two stars is rarely needed and hard to read.
Create int x = 5;, a pointer p to it, and a double pointer pp to p. Print x, *p and **pp to confirm all three show 5, then change the value using **pp and print again.
Summary
- A pointer to pointer stores the address of another pointer.
- Declare it with
**; dereference twice to reach the value. *ppgives the inner pointer,**ppgives the final value.- Double pointers let a function change the caller's own pointer.
- Used for string arrays, dynamic 2D arrays and linked structures.
Frequently Asked Questions
What is a pointer to pointer in C?
int **pp, and lets you work with a pointer indirectly, one level further out.Why would you use a double pointer?
What does ** mean in C?
* means "pointer to", so int * is a pointer to an int. Two of them, int **, means "pointer to a pointer to an int". When dereferencing, one * reaches the inner pointer and a second * reaches the final value.How do you get the value from a double pointer?
pp is a double pointer, then *pp gives the pointer it points to, and **pp gives the actual value at the end of the chain. Each * peels off one level.