# 14.9 – Pointer to pointer – Second indirection

by on January 22, 2018

## Pointer to pointer

We know that, a pointer is a variable which stores the address of another variable. If a variable is used to store the address of a pointer then the variable is called pointer to pointer.

While declaring a pointer we use * as suffix to the pointer whereas ** is used as suffix to the pointer to pointer. We need to use *** as a suffix to declare a pointer to store the address of pointer to pointer.

```short x=3465;
```

declaring an integer variable x and assigning it with 3465

```short *p;
p=&x;
```

p is the pointer to the variable x as it is holding the address of x. Though p is the pointer, it has its address.

```short **q;
q=&p;
```

Here q is the pointer to the pointer p as it is holding the address of p. Now x is a normal variable assigned with 3465, p is a pointer to x as p stores the address of x, q is a pointer to p as q stores the address of p

We can access the value of x by its pointer to pointer q using second indirection Now we will see how we can access values and addresses at different levels

```printf(“%d”,x);
```

Prints the value of x

```printf(“%u”,&x);
```

prints the address of x

```printf(“%u”,p);
```

Prints the address of x because p is the pointer to x, which holds the address of x

```printf(“%d”,*p);
```

Value at p is the value at 65522 that would be the value of x

```printf(“%u”,&p);
```

Though p is the pointer, it has its address. The statement prints the address of p

```printf(“%u”,q);
```

Prints the address of p because q is the pointer to p, holds the address of p

```printf(“%u”,*q);
```

Value at q is the value at 65524 that would be the value of p which is the address of x

```printf(“%d”,**q);
```

Prints the value of x ```#include<stdio.h>
int main()
{
int x=3465,*p,**q;
p=&x;
q=&p;
printf("Value of x %d",x);
printf("\nAddress of x %u",&x);
printf("\nAddress of x %u",p);
printf("\nValue of x %d",*p);
printf("\nAddress of p %u",&p);
printf("\nAddress of p %u",q);
printf("\nValue of p %u",*q);
printf("\nValue of x %d",**q);
return 0;
}
```

Output:
Value of x 3465
Address of x 65524
Address of x 65524
Value of x 3465
Address of p 65522
Address of p 65522
Value of p 65524
Value of x 3465

A variable may have a chain of pointers. It is possible to access the value through any pointer at any level using dereferencing ```short x=10;
short *p,**q,***r;
p=&x;
q=&p;
r=&q;
```

Here p is the pointer to x, q is the pointer to p and r is the pointer to q. The value of x can be access as x, *p, **q and ***r;

```#include<stdio.h>
int main()
{
short x=10,*p=&x,**q=&p,***r=&q;
printf("%d",x);
printf("\n%d",*p);
printf("\n%d",**q);
printf("\n%d",***r);
return 0;
}
```

Output:
10
10
10
10

Previous post: