Pointer to Pointer or Double Pointer Example Program In C++


Definition

  • Pointer to Pointer locates/store to another pointer variable address.
  • The dereference operator or indirection operator, noted by an asterisk ("*"), is also a unary operator in c languages that uses for pointer variables.
  • Double Pointer or Pointer to Pointer need to place a ** before the name of the double pointer.

Syntax

data_type **pointer_variable;

Pointer to Pointer or Double Pointer Example Program

/*
Pointer to Pointer locates/store to another pointer variable address.
The dereference operator or indirection operator, noted by asterisk ("*"), is also a unary operator in c languages that uses for pointer variables.
Double Pointer or Pointer to Pointer need to place an ** before the name of double pointer.
 */

/*##C++ Pointer to Pointer or Double Pointer Example Program with Reference operator (&) and Double Dereference operator (**)*/
/*##Simple Pointer C++ Programs,C++ Pointer to Pointer,C++ Double Pointer*/


// Header Files
#include <iostream>
#include<conio.h>

using namespace std;

int main() {

   int var;
   //Pointer Variable Declaration for Integer Data Type 
   int *pt;

   //Double Pointer Variable Declaration with Double Dereference operator (**)
   int **dp;

   cout << "Pointer Example C++ Program : Pointer to Pointer or Double Pointer  \n";

   var = 100;

   cout << "Address of var 			[&var ]	:" << &var << "\n";
   cout << "Value   of var 			[var ]	:" << var << "\n\n";

   //& takes the address of var , Here now pt == &var, so *pt == var
   pt = &var;

   cout << "Address of Pointer 		[pt  ] 	:" << pt << "\n";
   cout << "Value of Pointer 		[*pt ] 	:" << *pt << "\n\n";

   //& takes the address of pt , Here now dp == &pt, so *pt == pt and **dp==var
   dp = &pt;

   cout << "Address of Double Pointer 	[dp  ] 	:" << dp << "\n";
   cout << "Value of Double Pointer 		[*dp ] 	:" << *dp << "\n\n";
   cout << "Double Pointer Value 		[**dp] 	:" << **dp << "\n";

   getch();
   return 0;

}

Sample Output

Pointer Example C++ Program : Pointer to Pointer or Double Pointer

Address of var                  [&var ] :0x6ffe34
Value   of var                  [var ]  :100

Address of Pointer              [pt  ]  :0x6ffe34
Value of Pointer                [*pt ]  :100

Address of Double Pointer       [dp  ]  :0x6ffe28
Value of Double Pointer                 [*dp ]  :0x6ffe34

Double Pointer Value            [**dp]  :100