Pointer to Pointer

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

C++Intermediate
C++
#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

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

#include <iostream>

This header provides:

  • cout for displaying output
  • cin for taking input from the user

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 = # // 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.