Pointer to Pointer in C++ Programming Language

Author: Al-mamun Sarkar Date: 2020-04-03 17:04:36

Pointer to Pointer in C++ Programming Language. The following code shows how to use pointer to pointer in C plus plus Programming Language.

 

Code:

#include<iostream>
using namespace std;

int main()
{
    int x = 5;
    int *p = &x;
    int **q = &p;
    int ***r = &q;

    cout << "Output of p " << p << endl;
    cout << "Output of *p " << *p << endl;
    cout << "Output of *q " << *q << endl;
    cout << "Output of **q " << *(*q) << endl;
    cout << "Output of ***r " << *(*r) << endl;
    cout << "Output of ***r " << *(*(*r)) << endl;

    // Change value of x by r pointer
    ***r = 152;
    cout << "Output of x " << x << endl;

    // Change value of x by r pointer
    **q = 280;
    cout << "Output of x " << x << endl;

    // Change value of x by p pointer
    *p = 384;
    cout << "Output of x " << x << endl;

    // Change value of x by x
    *p = 785;
    cout << "Output of x " << x << endl;

    return 0;
}

 

Output:

Output of p 0x7ffee1eada68
Output of *p 5
Output of *q 0x7ffee1eada68
Output of **q 5
Output of ***r 0x7ffee1eada68
Output of ***r 5
Output of x 152
Output of x 280
Output of x 384
Output of x 785