Pointer on Pointers in C language

In C language it is possible to make a pointer to point to another pointer that points to the target value, thus creating a chain of pointers. This situation is called multiple indirection, or pointers to pointers. The below figure will helps to clarify the concept of multiple indirection.


As we know that the value of a normal pointer is the address of the object that contains the desired value. But in the case of of a pointer to pointer, the first pointer contains the address of the second pointer, which points to the object that contains the desired value.


At above figure, the pointer variable ptr2 contains the address of the pointer variable ptr1, which points to the location that contains the desired value.

A variable that is a pointer to a pointer must be declared using additional indirection operator symbols in front of the name. The syntax is  :
 data_type **pointer_name;
For example :
 int **ptr2;
The declaration tells the compiler that ptr2 is a pointer to a pointer of int type. Also note that the pointer ptr2 is not a pointer to an integer, but rather a pointer to an integer pointer.

Now to access the target value indirectly pointed to by pointer to a  pointer by applying the indirection operator twice.
 **ptr2;
Now lets see a C example code :
#include <stdio.h>

int main() {
  int num = 50;
  int*ptr1;
  int **ptr2;

  ptr1 = &num;
  ptr2 = &ptr1;

  printf("Value of num is : %d\n", **ptr2);
  return 0;
}
Output :

Value of num is : 50


Next Topic :