C++ Pointer to Pointer

As we know by now that a pointer stores the address of the pointed variable. But it is not the only use, pointer also stores the address of another pointer forming a chain like structure.

When we defined the pointer to a pointer, it means the first pointer contains the address of the second pointer and the second pointer contains the address of an actual variable.

Pointer to Pointer

We can declare the pointer to pointer variable by simply adding an additional asterisk * before the pointer name. example:

int **pointer_name;

C++ Program for Pointer to Pointer

#include <iostream>
using namespace std;
 
int main () 
{
   int num = 100;
    
   //Pointer declaration
   int  var;
   int  *ptr1;
   int  **ptr2;

   //assigning address to pointer
   ptr1 = &num;
   ptr2 = &ptr1;

   // value present in each varibles
   cout << "Value stored by var :" << num << endl;
   cout << "Value stored byt *ptr1 :" << *ptr1 << endl;
   cout << "Value stored by **ptr2 :" << **ptr2 << endl;

   return 0;
}

Output: After execution, the following output will be displayed.

Value stored by var :100
Value stored byt *ptr1 :100
Value stored by **ptr2 :100