Chain of pointers

Chain of pointers 

It is possible to make a pointer to point to another pointer, thus creating a chain of pointers as shown.

      P2                            P1                   Variable 
 Address 2-------->address--------->value 

Here, the pointer variable p2 contains the address of the pointer variable p1, which points to the location that contains the desired value. This is known as multiple indirections.

A variable that is a pointer to a pointer must be declared using additional indirection operator symbols in front of the name.  

Example:

Int**P2;

This declaration tells the compiler that p2 is a pointer to a pointer of int type. Remember, the pointer p2 is not a pointer to an integer, but rather a pointer to an integer pointer. 

We can access the target value indirectly pointed to by pointer to a pointer by applying the indirection operator twice. Consider the following code:

main ()
{
int x, *p1, **p2;
X = 100;
pl = &x; /* address of x */
p2 = &p1 /* address of p1 */
printf ("%d", **p2);
}

This code will display the value 100. Here, p1 is declared as a pointer to an integer and p2 as a pointer to a pointer to an integer. 
Posted on by