Latest web development tutorials

C ++ pointer to a pointer (multilevel indirect addressing)

C ++ pointers C ++ pointers

The pointer is a pointer to a multi-stage form of indirect addressing, or is a pointer chain. Typically, a pointer contains the address of a variable. When we define a pointer to a pointer, the first pointer contains the address of the second pointer, the second pointer contains the location of the actual value.

C ++, a pointer to a pointer

A pointer to a pointer variable must be declared as follows, namely placing two asterisks in front of the variable name. For example, the following declares a pointer to an int pointer pointers:

int **var;

When a target is a pointer to another pointer indirectly points to access this value requires the use of two asterisks operator, as shown in the following examples:

#include <iostream>
 
using namespace std;
 
int main ()
{
   int  var;
   int  *ptr;
   int  **pptr;

   var = 3000;

   // 获取 var 的地址
   ptr = &var;

   // 使用运算符 & 获取 ptr 的地址
   pptr = &ptr;

   // 使用 pptr 获取值
   cout << "Value of var :" << var << endl;
   cout << "Value available at *ptr :" << *ptr << endl;
   cout << "Value available at **pptr :" << **pptr << endl;

   return 0;
}

When the above code is compiled and executed, it produces the following results:

Value of var = 3000
Value available at *ptr = 3000
Value available at **pptr = 3000

C ++ pointers C ++ pointers