Double Pointers

Definition

  • A double pointer is a pointer that holds the memory address of another pointer.
  • In other words, it's a pointer to a pointer, allowing indirect access to a memory location where another pointer is stored.

It is denoted by two asterisks (**).

f1r4qp4b.png

Syntax:

Declaring a double pointer involves using two asterisks (**) before the variable name.

Example:

int** p_to_p;

Working of Double Pointer

Suppose we have the following code snippet:

int main() {
    int x = 42; // Create an integer variable
    int* p = &x; // Create a pointer and assign the address of x to it
    int** pp = &p; // Create a double pointer and assign the address of p to it

    // Print the value of x using double pointer
    std::cout << "Value of x using double pointer: " << **pp << std::endl;

    return 0;
}

Explanation of each step:

  1. Declaration:
    1. We declare a double pointer pp using the syntax int** pp;, indicating that pp is a pointer to a pointer to an integer.
  2. Initialization:
    1. We initialize the double pointer pp by assigning the address of the pointer p to it using the syntax int** pp = &p;. Now, pp holds the memory address of p.
  3. Dereferencing:
    1. To access the value of x using the double pointer pp, we use the dereference operator (*) twice: **pp.
    2. The first dereference (*pp) retrieves the address of the pointer p.
    3. The second dereference (**pp) retrieves the value stored at the address pointed to by p, which is the value of x.
  4. Output:
    1. We print the value of x using the double pointer: std::cout << "Value of x using double pointer: " << **pp << std::endl;.
working-of-double-pointers.png

Size of Pointer to Pointer

The size of a pointer to a pointer (double pointer) in C/C++ is the same as the size of a normal pointer. This is because a pointer to a pointer stores the memory address of another pointer, and the memory address itself is of fixed size, irrespective of the type of data it points to.

Here's a simple program to verify this:

#include <stdio.h>

int main() {
    int* p;
    int** pp;

    printf("Size of int* (pointer): %lu bytes\n", sizeof(p));
    printf("Size of int** (double pointer): %lu bytes\n", sizeof(pp));

    return 0;
}

// Output
Size of int* (pointer): 8 bytes
Size of int** (double pointer): 8 bytes

Complete Example:

#include <iostream>
using namespace std;

int main() {
    int x = 07; // Create an integer variable
    int* p = &x; // Create a pointer and assign the address of x to it
    int** pp = &p; // Create a double pointer and assign the address of p to it

    cout << "address of x: " << &x << endl;
    cout << "address of x: stored in p " << p << endl;
    cout << "address of x: stored in double p " << *pp << endl;

    cout << "Printing Value Now"<< endl;
    cout << "value of x: " << x << endl;
    cout << "value of x: by pointer p " << *p << endl;
    cout << "value of x: by double-pointer pp " << **pp << endl;

    return 0;
}

// Output
address of x: 0x7ffdfea609a4
address of x: stored in p 0x7ffdfea609a4
address of x: stored in double p 0x7ffdfea609a4
Printing Value Now
value of x: 7
value of x: by pointer p 7
value of x: by double-pointer pp 7