1. Intro
A pointer to pointer (also called a double pointer) is a pointer variable that stores the address of another pointer, rather than the address of an ordinary variable. Since a pointer variable itself lives at some address in memory, another pointer can point to it, creating a chain of indirection.
Cricket analogy: A team manager holds the phone number of the captain, who in turn holds the batting order — the manager doesn't know the batting order directly but reaches it through the captain, just as a double pointer reaches a value through another pointer.
2. Syntax
int** pp; // declares a pointer to a pointer to int
int x = 5;
int* p = &x; // p points to x
pp = &p; // pp points to p
**pp; // dereference twice to reach x's value3. Explanation
Declaring int** pp; creates a variable pp whose job is to hold the address of an int* pointer. To get from pp all the way down to the original int value, you need to dereference twice: *pp gives you back the pointer p (an int*, i.e. an address), and **pp gives you the actual int value that p points to. Each * "peels off" one level of indirection. Pointer-to-pointer is commonly used for dynamically allocated 2D arrays, for functions that need to modify a caller's pointer (not just the value it points to), and for arrays of strings/pointers such as char** argv in main.
Cricket analogy: Declaring int** pp is like a scorer's master sheet that references a specific over sheet (*pp gets you the over sheet reference), and reading that over sheet (**pp) finally gives you Rohit Sharma's actual runs scored.
4. Example
#include <iostream>
using namespace std;
int main() {
int x = 5;
int* p = &x; // p points to x
int** pp = &p; // pp points to p
cout << "x = " << x << endl;
cout << "*p = " << *p << endl;
cout << "**pp = " << **pp << endl;
**pp = 99; // modifies x through two levels of indirection
cout << "After **pp = 99, x = " << x << endl;
return 0;
}5. Output
x = 5
*p = 5
**pp = 5
After **pp = 99, x = 996. Key Takeaways
- A pointer to pointer stores the address of another pointer, declared as
int** pp;. *ppgives the pointer it stores (p);**ppdereferences twice to reach the original value.- Each level of
*peels off one layer of indirection. - Double pointers are used for dynamic 2D arrays, modifying a caller's pointer, and arrays of pointers (e.g.
char** argv).
Practice what you learned
1. What does a pointer-to-pointer variable store?
2. How do you declare a pointer to a pointer to `int`?
3. Given `int x = 5; int* p = &x; int** pp = &p;`, what does `*pp` evaluate to?
4. In the same setup, what does `**pp` evaluate to?
5. Which of these is a typical real-world use of pointer-to-pointer?
Was this page helpful?
You May Also Like
Pointers in C++
Learn what a pointer is, how to declare one, and how the `&` and `*` operators let you access and manipulate memory addresses directly.
Dynamic Memory Allocation in C++
Learn how `new` and `delete` allocate and free heap memory at runtime, and why forgetting to `delete` causes a memory leak.
Pointers and Functions in C++
Learn how passing a pointer to a function lets it modify the caller's original variable, unlike ordinary pass-by-value arguments.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics