Thursday, 7 February 2019

Pointer Pointer / Pointer to Pointer / Double Pointer

When a pointer variable store address of another pointer variable is known as pointer pointer or pointer to pointer or double pointer in C. First pointer store the address of the variable and second pointer store address of the first pointer.

Declaration of pointer pointer (double pointer)

int *ptr;

Initialization of pointer pointer (double pointer)

int a = 10;
int *ptr, **q;
ptr =&a;
q = &ptr //initialization of pointer variable q

Pointer in C
Types of pointers in C
Dangling Pointer
Function Pointer

C program to demonstrate pointer pointer (double pointer)

#include <stdio.h>
int main ( )
int a = 10; //Declaration and initialization of variable
int *ptr; //Declaration of pointer vaiable ptr (ptr is a single pointer)
int **q; // Declaration of  pointer variable q (q is a double pointer)
ptr = &a; // Initialization of pointer variable ptr
q = &ptr; // Initialization of pointer variable q
printf ("value of ptr=%d\n value of q=%d\n", *ptr, **q);
printf ("q=%d", *q);
return 0;

value of ptr =10
value of q = 10
q = 2686744

Explanation of the above program
int a = 10;

pointer pointer

int *ptr
pointer pointer

int **q;
pointer pointer

ptr = &a;
= &ptr;
pointer pointer

In the above program, the variable 'a' hold the value '10'. Pointer variable 'ptr' hold the address of the variable 'a' that is 26869 and pointer variable 'q' hold the address of pointer variable 'ptr' that is 40000.  If we want to access the value of the variable 'a' by pointer variable 'ptr' and 'q' then we using int *ptr and int **q. 

How int  **q; access the value of the variable 'a'?

int **q;

pointer 'q' hold the address of the pointer 'ptr' and pointer 'ptr' hold the address of the variable 'a'.

** ==> *(40000) ==> 10 


**q ==> *(address of *ptr) ==> (value of variable 'a' = 10)

Recommended Posts

Structure in C
Union in C
Strings in C

No comments:

Post a Comment