Skip to content
Related Articles
Open in App
Not now

Related Articles

C++ Pointer To Pointer (Double Pointer)

Improve Article
Save Article
  • Difficulty Level : Basic
  • Last Updated : 27 Jan, 2023
Improve Article
Save Article

In C++ a Pointer is a variable that is used to store the memory address of other variables. It is a  variable that points to a data type (like int or string) of the same type and is created with the * operator.

Syntax of a Pointer in C++:

data_type_of_pointer *name_of_variable = & normal_variable;

What is a Pointer to a Pointer or Double Pointer in C++?

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

 

The above diagram shows the memory representation of a Pointer to Pointer or a Double Pointer, we can easily understand that the address of the variable (i.e Address 1) is stored in Pointer 1 and the address of Pointer 1(i.e Address 2) is stored in Pointer 2. This is known as Double Pointers or Pointer to 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:

int val = 169;

int *ptr = &val; // storing address of val to pointer ptr.

int **double_ptr = &ptr; // pointer to a pointer declared which is pointing to an integer.

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?

 

Below is the C++ Program to implement Pointer to Pointer:

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

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.

Time complexity: O(1).
Auxiliary space: O(1).


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!