Open In App

C++ Pointer To Pointer (Double Pointer)

Last Updated : 02 Aug, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Now, we already know that a pointer stores the memory address of other variables. So, when we define a pointer to a pointer, the first pointer is used to store the address of the variables, and the second pointer stores the address of the first pointer. For this very reason, this is known as a Double Pointer or Pointer to Pointer.

The below diagram explains the concept of Double Pointers

Double Pointer
 

In a double pointer, Pointer 1 stores the address of a variable, and Pointer 2 stores the address of Pointer 1. This setup is called a pointer to pointer or double pointer.

How to Declare a Pointer to a Pointer in C ++?

Declaring a Pointer to Pointer is similar to declaring a pointer in C++. The difference is we have to use an additional * operator before the name of a Pointer in C++.

Syntax of a Pointer to Pointer(Double Pointer) in C++:

data_type_of_pointer **name_of_variable = & normal_pointer_variable;

Example:

C++
// C++ program to implement 
// pointer to pointer
#include <bits/stdc++.h>
using namespace std;

// Driver code
int main()
{
  int variable = 169;
  
  // Pointer to store the address 
  // of variable
  int* pointer1;

  // double pointer to store the 
  // address of pointer1
  int** pointer2;

  // Storing address of variable 
  // in pointer1
  pointer1 = &variable;

  // Storing address of pointer1 
  // in pointer2
  pointer2 = &pointer1;

  // Displaying the value of variable 
  // with using both single and double 
  // pointers.
  cout << "Value of variable :- " << 
           variable << "\n";
  cout << "Value of variable using single pointer :- " << 
           *pointer1 << "\n";
  cout << "Value of variable using double pointer :- " << 
           **pointer2 << "\n";
  return 0;
}

Output
Value of variable :- 169
Value of variable using single pointer :- 169
Value of variable using double pointer :- 169

The below diagram explains the concept of Double Pointers: 

How does Double Pointer works?
 

The above diagram shows the memory representation of a pointer to a pointer. The first pointer ptr1 stores the address of the variable and the second pointer ptr2 stores the address of the first pointer. 

Example: How does Double Pointer works?

What will be the size of a pointer to a pointer in C++?

In the C++ programming language double pointer behave similarly to a normal pointer. So, the size of the variable of the double-pointer and the size of the normal pointer variable is always equal.

Below is a C++ program to check the size of a double pointer:

C++
// C++ program to check the size 
// of a pointer to a pointer.
#include <bits/stdc++.h>
using namespace std;

// Driver code
int main()
{
  int val = 169;
  int* ptr = &val;
  int** double_ptr = &ptr;

  cout << " Size of normal Pointer: " << 
            sizeof(ptr) << "\n";
  cout << " Size of double Pointer: " << 
            sizeof(double_ptr) << "\n";
  return 0;
}

Output
 Size of normal Pointer: 8
 Size of double Pointer: 8

Note: The output of the above code also depends on the type of machine which is being used. The size of a pointer is not fixed in the C++ programming language and it totally depends on other factors like CPU architecture and OS used. Usually, for a 64-bit Operating System, a size of 8 bytes memory and for a 32-bit Operating system, a size of 4 bytes memory is assigned.


Similar Reads