Pointer to Pointer in C++
Understand double pointers in C++: an int** that stores the address of an int*. Follow *ptrToPtr and **ptrToPtr step by step and see where they are used.
1#include <iostream>2using namespace std;34int main() {5 int num = 10;6 int* ptr = # // Pointer to int7 int** ptrToPtr = &ptr; // Pointer to pointer89 cout << "Value of num: " << num << endl;10 cout << "Address of num: " << &num << endl;1112 cout << "\nValue of ptr (address of num): " << ptr << endl;13 cout << "Address of ptr: " << &ptr << endl;14 cout << "Value pointed by ptr: " << *ptr << endl;1516 cout << "\nValue of ptrToPtr (address of ptr): " << ptrToPtr << endl;17 cout << "Address of ptrToPtr: " << &ptrToPtr << endl;18 cout << "Value pointed by ptrToPtr: " << *ptrToPtr << endl;19 cout << "Value pointed by *ptrToPtr: " << **ptrToPtr << endl;2021 // Modify value using double pointer22 **ptrToPtr = 20;23 cout << "\nAfter modifying through double pointer:" << endl;24 cout << "Value of num: " << num << endl;2526 return 0;27}
Sample Output
Value pointed by *ptrToPtr: 10
Two dereferences.
Value of num: 20
Changed through **ptrToPtr.
In this one
- How int** stores the address of an int*
- What one and two dereferences give
- How **pp = value changes the original variable
- Where double pointers appear in real code
How it works
- 1
num = 10, ptr = &num, ptrToPtr = &ptr.
- 2
Print each variable's value and address.
- 3
*ptrToPtr gives ptr's value (num's address).
- 4
**ptrToPtr gives 10.
- 5
**ptrToPtr = 20 changes num.
Edge cases
Type mismatch
Write int** pp = #
&num is an int*, not an int**, so the compiler rejects it.
Dangling chain
Let ptr go out of scope while ptrToPtr still exists.
ptrToPtr now points at memory that no longer holds ptr, and following it is undefined.
What's going on
A pointer to a pointer stores the address of another pointer. Here num holds 10, ptr holds the address of num, and ptrToPtr holds the address of ptr. It is a two-step path to the same int.
`int
ptrToPtr = &ptr;` reads as "pointer to (pointer to int)". The type must match: `&ptr` is an `int
`, so it fits.
The prints line up like a chain. ptrToPtr equals &ptr. *ptrToPtr follows one step and gives the value inside ptr, which is the address of num. **ptrToPtr follows two steps and gives 10.
**ptrToPtr = 20; walks both steps and writes into num, so num prints 20 at the end.
Why would anyone want this? The common case is a function that must change a caller's pointer. If you pass int* p by value, the function gets a copy and cannot change where the caller's pointer points. Passing `int
pp` (or, in C++, `int*& p`) lets it do `*pp = new int(5);`. Another case is a 2D array allocated as rows of pointers, or `char
argv in main(int argc, char** argv)`.
In everyday modern C++, references and containers like vector<vector<int>> remove most of the need, but you will still meet ** in older code and C libraries.
The only header is <iostream>. It gives you cout for displaying output and cin for taking input from the user.
Understanding Pointer to Pointer
Double Pointer Concept:
Memory Layout:
num (value: 10)
↑
ptr (points to num)
↑
ptrToPtr (points to ptr)
Declaring Double Pointer
Syntax:
dataType** pointerName;
Example:
int num = 10;
int* ptr = # // Single pointer
int** ptrToPtr = &ptr; // Double pointer
What happens here:
Two-Level Dereferencing
Accessing Values:
*ptrToPtr // Gets value of ptr (address of num)
**ptrToPtr // Gets value of num (10)
What happens here:
Modifying Through Double Pointer
Modifying Value:
**ptrToPtr = 20; // Changes num to 20
What happens here:
When to Use Double Pointers
Best For:
Example Scenarios:
Dynamic 2D Arrays
Example:
int** matrix = new int*[rows];
for (int i = 0; i < rows; i++) {
matrix[i] = new int[cols];
}
What happens here:
Modifying Pointer in Function
Problem: Function needs to modify pointer value
Solution: Pass pointer to pointer
void allocateMemory(int** ptr) {
*ptr = new int(42); // Modifies original pointer
}
int* myPtr;
allocateMemory(&myPtr); // Pass address of pointer
Important Considerations
Dereferencing Levels:
Memory Management:
Complexity:
This ends the program successfully.
1return 0;
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
If int x = 1; int* p = &x; int** q = &p; what is **q?
Pick an answer to check it.
Question 2
Why pass int** to a function?
Pick an answer to check it.
Questions people ask
How many levels can I go?
As many as you like (int***), but more than two is rarely readable.
What is int*& ?
A reference to a pointer. It lets a function change the caller's pointer without double-pointer syntax.
Why is argv a char**?
It is an array of C-strings, and each C-string is a char*.