Pointer to Pointer
Pointer to Pointer (Double Pointer) Program in C++
C++ Pointer to Pointer Program
This program helps you to learn the fundamental structure and syntax of C++ programming.
#include <iostream>
using namespace std;
int main() {
int num = 10;
int* ptr = # // Pointer to int
int** ptrToPtr = &ptr; // Pointer to pointer
cout << "Value of num: " << num << endl;
cout << "Address of num: " << &num << endl;
cout << "\nValue of ptr (address of num): " << ptr << endl;
cout << "Address of ptr: " << &ptr << endl;
cout << "Value pointed by ptr: " << *ptr << endl;
cout << "\nValue of ptrToPtr (address of ptr): " << ptrToPtr << endl;
cout << "Address of ptrToPtr: " << &ptrToPtr << endl;
cout << "Value pointed by ptrToPtr: " << *ptrToPtr << endl;
cout << "Value pointed by *ptrToPtr: " << **ptrToPtr << endl;
// Modify value using double pointer
**ptrToPtr = 20;
cout << "\nAfter modifying through double pointer:" << endl;
cout << "Value of num: " << num << endl;
return 0;
}Value of num: 10 Address of num: 0x7fff5fbff6ac Value of ptr (address of num): 0x7fff5fbff6ac Address of ptr: 0x7fff5fbff6a0 Value pointed by ptr: 10 Value of ptrToPtr (address of ptr): 0x7fff5fbff6a0 Address of ptrToPtr: 0x7fff5fbff698 Value pointed by ptrToPtr: 0x7fff5fbff6ac Value pointed by *ptrToPtr: 10 After modifying through double pointer: Value of num: 20
Understanding Pointer to Pointer
This program teaches you how to use Pointer to Pointer (Double Pointer) in C++. A pointer to pointer stores the address of another pointer, creating a two-level indirection. Double pointers are declared with ** and require two levels of dereferencing to access the actual value.
---
1. What This Program Does
The program demonstrates pointer to pointer operations:
Double pointers enable advanced memory management and data structures.
---
2. Header File Used
This header provides:
---
#include <iostream>3. Understanding Pointer to Pointer
Double Pointer Concept
:
Memory Layout
:
↑
ptr (points to num)
↑
ptrToPtr (points to ptr)
---
4. Declaring Double Pointer
Syntax
:
dataType** pointerName;
Example
:
int num = 10;
int* ptr = # // Single pointer
int** ptrToPtr = &ptr; // Double pointer
How it works
:
---
5. Two-Level Dereferencing
Accessing Values
:
*ptrToPtr // Gets value of ptr (address of num)
**ptrToPtr // Gets value of num (10)
How it works
:
---
6. Modifying Through Double Pointer
Modifying Value
:
**ptrToPtr = 20; // Changes num to 20
How it works
:
---
7. When to Use Double Pointers
Best For
:
Example Scenarios
:
---
8. Dynamic 2D Arrays
Example
:
int** matrix = new int*[rows];
for (int i = 0; i < rows; i++) {
matrix[i] = new int[cols];
}
How it works
:
---
9. 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
---
10. Important Considerations
Dereferencing Levels
:
Memory Management
:
Complexity
:
---
11. return 0;
This ends the program successfully.
---
Summary
This program is fundamental for learning advanced pointer concepts, understanding multi-level indirection, and preparing for dynamic memory allocation and complex data structures in C++ programs.
Let us now understand every line and the components of the above program.
Note: To write and run C++ programs, you need to set up the local environment on your computer. Refer to the complete article Setting up C++ Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your C++ programs.
Practical Learning Notes for Pointer to Pointer
This C++ program is part of the "Memory Management Programs" topic and is designed to help you build real problem-solving confidence, not just memorize syntax. Start by understanding the goal of the program in plain language, then trace the logic line by line with a custom input of your own. Once you can predict the output before running the code, your understanding becomes much stronger.
A reliable practice pattern is to run the original version first, then modify only one condition or variable at a time. Observe how that single change affects control flow and output. This deliberate style helps you understand loops, conditions, and data movement much faster than copying full solutions repeatedly.
For interview preparation, explain this solution in three layers: the high-level approach, the step-by-step execution, and the time-space tradeoff. If you can teach these three layers clearly, you are ready to solve close variations of this problem under time pressure.