Pointer to Pointer in C++

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

IntermediateMemory Management ProgramsExample 3 of 11
pointer-to-pointer.cpp
1#include <iostream>
2using namespace std;
3
4int main() {
5 int num = 10;
6 int* ptr = &num; // Pointer to int
7 int** ptrToPtr = &ptr; // Pointer to pointer
8
9 cout << "Value of num: " << num << endl;
10 cout << "Address of num: " << &num << endl;
11
12 cout << "\nValue of ptr (address of num): " << ptr << endl;
13 cout << "Address of ptr: " << &ptr << endl;
14 cout << "Value pointed by ptr: " << *ptr << endl;
15
16 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;
20
21 // Modify value using double pointer
22 **ptrToPtr = 20;
23 cout << "\nAfter modifying through double pointer:" << endl;
24 cout << "Value of num: " << num << endl;
25
26 return 0;
27}

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

What's going on

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.

Header File Used

This header provides:

cout for displaying output
cin for taking input from the user
1#include <iostream>

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)

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

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

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

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

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

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

:

*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)

return 0;

This ends the program successfully.