Course offer · HLD · LLD · System Design at ₹799 until 30 Nov (not interview packs)

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.

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}

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. 1

    num = 10, ptr = &num, ptrToPtr = &ptr.

  2. 2

    Print each variable's value and address.

  3. 3

    *ptrToPtr gives ptr's value (num's address).

  4. 4

    **ptrToPtr gives 10.

  5. 5

    **ptrToPtr = 20 changes num.

Edge cases

Type mismatch

Write int** pp = &num;

&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:

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

What happens here:

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

What happens here:

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

What happens here:

**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];

}

What happens here:

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)

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