Multiple Indirections

Multiple indirections are called pointer of pointer or chain of pointer. So how to make a chain of pointer and how to use it is explained below. Here we will learn up to two levels of a pointer. For more levels, we need more indirection operators to use.

Syntax of declaring pointer of pointer

Syntax of the declaration is the same as of simple pointer but here we need two indirection operators. Because it is the second level of a pointer. (For the 3rd level of pointer we will need 3 indirection operators). Assignment syntax is the same as a simple pointer.

<data type of pointer>  **<name of 2nd level pointer>;

How to create a pointer of pointer?

Suppose a pointer ‘p’ points to one variable ‘a’ means it holds the address of that ‘a’ variable. Now if we want to store the address of that pointer ‘p’ somewhere then we have to make pointer ‘p1’ of that pointer ‘p’. So we can say ‘p’ is a pointer of ‘a’, and ‘p1’ is a pointer of ‘p’. An example of the same is as written below.

int a;
int *p=&a;      // p is pointer of a
int **p1 = &p;  // p1 is pointer of p

Now suppose ‘a’ is having value 2, address of ‘a’ is 6487572, address of ‘p’ is 6487560. Now understand it with the help of a diagram.

Summary Table

Below is the summary of the above example. By observing the above diagram, we can understand the below table.

Variable/Pointer

Variable/Pointer

Variable/Pointer

Value

**p1

*p

a

2

*p1

p

&a

6487572

P1

&p

 

6487560

Example program to prove above table

In the below program, ‘a’ is having value 2, address of ‘a’ is 6487572, address of ‘p’ is 6487560. ‘p’ is a pointer of ‘a’, and ‘p1’ is the pointer of ‘p’. The same program (as above) is written below and output is also shown. Which is the same as the above table.

 

#include<stdio.h>
#include<conio.h>
void main()
{
  int a=2;
  int *p=&a; // p is pointer of a
  int **p1 = &p;  // pp is pointer of p
  clrscr();
  printf("**p1 = %d, *p = %d ,a = %d \n",**p1,*p,a);
  printf("*p1= %u ,p= %u ,&a = %u \n",*p1,p,&a);
  printf("p1 =%u , &p = %u ",p1,&p);
  getch();
}

Output:

 

Learning from this blog:

  1. What are multiple indirections?
  2. Other names of multiple indirections.
  3. What is the need of a pointer and pointer of pointer?
  4. Example program to show pointer of pointer?
  5. Relation between variable, pointer and pointer of pointer?
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments