Easy Tutorial
❮ Cpp Multithreading Cpp Examples Calculator Switch Case ❯

C++ Pointer to Pointer (Multi-level Indirection)

C++ Pointers

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 another pointer.

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, which points to the location that contains the actual value.

A variable that is a pointer to a pointer must be declared as such, with two asterisks in front of the variable name. For example, the following declares a pointer to a pointer to an int:

int **var;

When a target value is indirectly pointed to by a pointer to a pointer, accessing this 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 the & operator
    pptr = &ptr;

    // Access the value using pptr
    cout << "Value of var :" << var << endl;
    cout << "Value of *ptr :" << *ptr << endl;
    cout << "Value of **pptr :" << **pptr << endl;

    return 0;
}

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

Value of var :3000
Value of *ptr :3000
Value of **pptr :3000

C++ Pointers

❮ Cpp Multithreading Cpp Examples Calculator Switch Case ❯