C++ Pointer to Pointer (Multi-level Indirection)
A pointer to a pointer is a form of multi-level indirection, or a chain of pointers.
A pointer to a pointer stores the address of a pointer in another pointer.
Normally, 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 containing the actual value.
A pointer-to-pointer variable must be declared as follows, by placing two asterisks before the variable name. For example, the following declares a pointer to a pointer of type int:
int **var;
When a target value is indirectly pointed to by one pointer through another pointer, accessing this value requires using the double asterisk operator, as shown in the example below:
Example
#include
<
iostream
using
namespace
std;
int
main
(
)
{
int
var
;
int
ptr
;
int
**
pptr
;
var
=
3000
;
// Get the address of var
ptr
= &
var
;
// Use the & operator to get the address of ptr
pptr
= &
ptr
;
// Use pptr to get the value
cout
<<
"
var value is :
"
<<
var
<<
endl
;
cout
<<
"
*ptr value is:
"
<< *
ptr
<<
endl
;
cout
<<
"
**pptr value is:
"
<< **
pptr
<<
endl
;
return
0
;
}
When the code above is compiled and executed, it produces the following result:
var 值为 :3000
*ptr 值为:3000
pptr value is:3000
C++ Pointers