Pointer-to-pointer-in C++ .

In this session i have discussed pointer to pointer in C++ .


The pointer variable is used to store the address of other variable . It is also store the address of other pointer this is called the pointer to pointer or double pointer or pointer chaining .


In the below diagram the ptr1 is double pointer because it stored the address of ptr2 pointer and ptr2 stored the address of a variable .

mvc image

Syntax

     int**ptr;
    

Example : Write a program that demonstrate the pointer to pointer .

    #include<iostream>
    using namespace std;
    int main()
    {  
          int x=10;
          int*ptr2; // normal pointer
          int**ptr1; // double pointer
          ptr2=&x;   // assign the address of x into ptr2
          ptr1=&ptr2; //assign the address of ptr2 into ptr1
          cout<<"\n x : "<<x;
          cout<<"\n ptr2 : "<<*ptr2;
          cout<<"\n ptr1 : "<<**ptr1;
          cout<<"\n address of x : "<<&x;
          cout<<"\n address of x using ptr2 : "<<ptr2;
          cout<<"\n address of x using ptr1 : "<<*ptr1;
          return 0;
    }
    
    OUTPUT : x : 10
             ptr2 : 10
             ptr1 : 10
             address of x : 0x20fef8
             address of x using ptr2 : 0x20fef8
             address of x using ptr1 : 0x20fef8
    

Welcome to AndroidATPoint.

back image                     next image