Pointer to Pointer

Pointer to Pointer (Double Pointer) Program in C++

IntermediateTopic: Memory Management Programs
Back

C++ Pointer to Pointer Program

This program helps you to learn the fundamental structure and syntax of C++ programming.

Try This Code
#include <iostream>
using namespace std;

int main() {
    int num = 10;
    int* ptr = &num;        // 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;
}
Output
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:

Declaring double pointer (int**)
Storing address of a pointer
Two-level dereferencing (**ptrToPtr)
Modifying values through double pointer

Double pointers enable advanced memory management and data structures.

---

2. Header File Used

This header provides:

cout for displaying output
cin for taking input from the user

---

#include <iostream>

3. Understanding Pointer to Pointer

Double Pointer Concept

:

Pointer that points to another pointer
Two levels of indirection
Declared with ** (two asterisks)
Requires two * operators to access value

Memory Layout

:

num (value: 10)

ptr (points to num)

ptrToPtr (points to ptr)

---

4. Declaring Double Pointer

Syntax

:

dataType** pointerName;

Example

:

int num = 10;

int* ptr = &num; // Single pointer

int** ptrToPtr = &ptr; // Double pointer

How it works

:

** indicates pointer to pointer
ptrToPtr stores address of ptr
ptr stores address of num

---

5. Two-Level Dereferencing

Accessing Values

:

*ptrToPtr // Gets value of ptr (address of num)

**ptrToPtr // Gets value of num (10)

How it works

:

First *: dereferences ptrToPtr → gets ptr
Second *: dereferences ptr → gets num
**ptrToPtr = *(*ptrToPtr) = *ptr = num

---

6. Modifying Through Double Pointer

Modifying Value

:

**ptrToPtr = 20; // Changes num to 20

How it works

:

**ptrToPtr accesses num through two levels
Can modify original variable
Useful when pointer itself needs to be modified

---

7. When to Use Double Pointers

Best For

:

Dynamic 2D arrays
Modifying pointer values in functions
Linked lists and trees
Passing pointers by reference
Advanced memory management

Example Scenarios

:

Allocating 2D arrays dynamically
Functions that modify pointer values
Linked list operations (modifying head pointer)
Tree data structures

---

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

:

matrix is pointer to pointer
Each matrix[i] is pointer to row
Enables dynamic 2D array allocation

---

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

:

*ptrToPtr: gets pointer value
**ptrToPtr: gets actual value
Must use correct number of * operators

Memory Management

:

Double pointers often used with dynamic memory
Requires careful cleanup
Each level may need separate delete

Complexity

:

More complex than single pointers
Use only when necessary
Consider alternatives (references, smart pointers)

---

11. return 0;

This ends the program successfully.

---

Summary

Pointer to pointer: stores address of another pointer, declared with **.
Two-level dereferencing: **ptrToPtr accesses value through two levels.
Useful for: dynamic 2D arrays, modifying pointers in functions, data structures.
**ptrToPtr = *(*ptrToPtr) accesses the actual value.
Understanding double pointers enables advanced memory management.
Essential for dynamic 2D arrays and complex data structures.

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.

Table of Contents