Pointer to Pointer in C++

As we know, a pointer variable stores the address of another variable. In this, a pointer variable which stores the address of the other variables than before, another pointer-variable store the address of this pointer-variable.

There is syntax below

data type **pointer-variable;


int **ptr;

How to access a pointer address in C++?

First of all, we declare a pointer-variable,

int *ptr1;

then declare a pointer-to-pointer variable and,

int **ptr2;

then assign the pointer-variable ptr1 to the pointer-variable ptr2, such as

ptr2 = &ptr1;

Let’s understand this with the help of a program

Pointer to Pointer Program in C++

In the program, the address of x variable is stored in one pointer-variable ptr1 while the address of this pointer-variable ptr1, is stored in another pointer variable ptr2. When this program executes, pointer-variable ptr1 will print the address of variable x. After this, the address of this pointer-variable ptr1, will print by another pointer variable ptr2.

using namespace std;
int main()
   int x=6;
   int *ptr1,**ptr2;

   ptr1 = &x; 
   ptr2 = &ptr1;

   cout<<"Values are:\n";
   cout<<"ptr1: "<<*ptr1<<" "<<**ptr2<<endl;

   cout<<"\nAddress of:\n";

   cout<<"*ptr2: "<<*ptr2<<endl;
   cout<<"ptr1: "<<ptr1<<endl;
   cout<<"ptr2: "<<ptr2;

return 0;


ptr1: 6 6
Address of:

*ptr2: 0x8f2bfff2
ptr1: 0x8f2bfff2
ptr2: 0x8f2bfff0


As you can see *ptr2 and ptr1 are displaying the same address in output This is because pointer variable ptr1 is printing the address of variable x. But *ptr2 is displaying store data in pointer variable ptr1 which is the address of variable x.

more about pointer

Previous- Pointer in C++ with example
Next- Memory allocation in C++

Like it?

Leave a Reply

Your email address will not be published.