C++ Pointer to Pointer (Multiple Indirection)

C++ Pointers

A pointer to a pointer is a form of multiple indirection, or a chain of pointers.

A pointer to a pointer is storing the address of a pointer inside another pointer.

Usually, 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, and the second pointer points to the location that holds the actual value.

C++ 中指向指针的指针

A pointer-to-pointer variable must be declared as follows, that is, placing two asterisks before the variable name. For example, the following declares a pointer to a pointer of int type:

int **var;

When a target value is indirectly pointed to by a pointer to another pointer, accessing that value requires the use of two asterisk operators, as shown in the following example:

Example

#include <iostream> using namespace std; int main () { int var; int *ptr; int **pptr; var = 3000; // get the address of var ptr = &var; // get the address of ptr using operator & pptr = &ptr; // use pptr to get the value cout << "var value :" << var << endl; cout << "*ptr value:" << *ptr << endl; cout << "**pptr value:" << **pptr << endl; return 0; }

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

var 值为 :3000
*ptr 值为:3000
**pptr 值为:3000

C++ Pointers

other extensions