Multiple Indirection

A pointer can point to another pointer that can point to the actual data item. This type of situation is called multiple indirection, or pointers to pointer. Figure illustrates the concept of multiple indirection. Normally the value pf the ordinary pointer holds the address of the variable which stores the desired value. But in case of pointer to pointer, the first pointer contains the address of the second pointer, which points to the variable that contains the desired value.

img

Multiple indirection can be carried on to whatever extent desired, but more than a pointer to a pointer is rarely needed. In fact, excessive indirection is difficult to follow and phone to conceptual error.

A variable that is a pointer to a pointer must be declared by preceding two asterisk symbols. For example, if dptr is a variable that points to a pointer variable, then the declaration will be as follows:

int **dptr;

Program – Illustration of pointer to pointer

#include<stdio.h>
#include<conio.h>
void main ()
{
    int x, *ptr, **dptr;
    clrscr ();
    x = 25;
    ptr = &x;
    dptr = &ptr;
    printf (“The value of x is %d\n”, x);
    printf (“The address of x is %u\n”, &x);
    printf (“The value of ptr is %u\n”, ptr);
    printf (“The address of ptr is %u\n”, &ptr);
    printf (“The value of dptr is %u\n”, dptr);
    printf (“The value of dptr is %u\n”, dptr);
    printf (“The address of dptr is %u\n”, &dptr);
    printf (“The value of x through ptr is %d\n”, *ptr);
    printf (“The value of x through dptr is %d\n”, **dptr);
    getch ();
}

Output

The value of x is 25
The address of x is 8886
The value of ptr is 8886
The address of ptr is 8882
The value of dptr is 8882
The address of dptr is 8878
The value of x through ptr is 25
The value of x through dptr is 25



About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.

We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc





 PreviousNext