Pointer to Pointer in C Language

Normally, a pointer is a variable that contains the address of another variable. When we define a pointer to a pointer, the first pointer contains the address of the second pointer, which points to the location that contains the actual value as shown below.

A variable that is a pointer to a pointer must be declared as such. This is done by placing an additional asterisk in front of its name. For example, the following declaration declares a pointer to a pointer of type int

1

int**var;

When a target value is indirectly pointed to by a pointer to a pointer, accessing that value requires that the asterisk operator (**) is applied twice.

Example of Pointer to Pointer

Let’s see the simple example of a pointer to pointer in C.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

#include <stdio.h>

intmain(){

intvar;

int*ptr;

int**pptr;

var=3000;

/* take the address of var */

ptr=&var;

/* take the address of ptr using address of operator & */

pptr=&ptr;

/* take the value using pptr */

printf("Value of var = %d\n",var);

printf("Value available at *ptr = %d\n",*ptr);

printf("Value available at **pptr = %d\n",**pptr);

return0;

}

When you run above program, the output will be following:

1

2

3

Value of var = 30

Value of *ptr = 30

Value of **pptr = 30

In above example, pptr contains the address of ptr and ptr contains the address of number variable var = 30.