C double pointer example

Program :

#include<stdio.h>
#include<conio.h>
void main() 
{
  int i =50;
  int **ptr1;
  int *ptr2;
  clrscr();
  ptr2 = &i;
  ptr1 = &ptr2;
  printf("\nThe value of **ptr1 : %d",**ptr1);
  printf("\nThe value of *ptr2  : %d",*ptr2);
  getch();
}

Output :

The value of **ptr1 : 50
The value of *ptr2  : 50

Explanation :

  1. Variable ‘i’ is initialized to 50.
  2. i is stored at Memory Location 2000.
  3. Pointer Variable ‘ptr2′ stores address of variable ‘i’.
*ptr2 will print [Value Stored at Address 2000 ] i.e 50
  1. Similarly ‘ptr1′ is also a pointer variable which stores the address of Pointer variable [i.e ptr2 stores address of integer variable while ptr1 stores address of another pointer variable].
  2. So
**ptr1 is used to access actual value.