pointer to pointer in c 

 As we can store the address of a normal variable in the pointer variable similarly, we can also store the address of one pointer variable into another pointer variable.

Example::

int i=10;

int *p;

int *q;

p=&i;

q=&p;


pointer-pointer-c

Above fig.1., shows three memory blocks of 2bytes.

The first block with address 6000  is declared as variable i. i is an integer variable assigned with value 10.

i=10;

The second block with address 7000 is declared as variable p. p is an unsigned pointer variable. As it is a pointer variable, it will store the address of another variable. Hence, it contains the address of integer variable i.

p=&i;

printf(“%d”,*p);

output::  10

The third block with address 8000 is named with variable q. q is an unsigned pointer variable. As it is a pointer variable, it will store the address of another variable. It stores the address of the pointer variable p.

q=&p;

Important point: A pointer variable can store the address of another pointer variable.

printf(“%u”, *q);

Output:: 7000.

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

Output:: 10.

Important point: whenever we print any address, we use format specifier as %u.  

Let's perform some operations on fig.1,

1.  printf(“%u”, &i);
    output:: 6000.
 
2.  printf(“%u”, p);
     output:: 6000
 
3.  printf(“%u”,&p);
     output:: 7000

 
4   printf(“%u”, q);
     output:: 7000.
 
5.  printf(“%u”, &q);
     output:: 8000.
 
6.  printf(“%u”, *q);
     output:: 6000.
 
7.  printf(“%d”, **q);
     output:: 10.
 
8.  printf(“%u”, *(&q));
     output:: 7000.
 
9.  printf(“%u”, **(&q));
     output:: 6000.
 
10. printf(“%d”,***( &q));
      output:: 10.

 

 Demo program

increment the number from 1 to 10 using pointer to pointer.


pointer-pointer-c

output::


pointer-pointer-c


 

see also::

 pointers in c

call by value and call by reference