Friday, July 29, 2016

C Basics - Pointer to Pointer in C

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

Pointer(*)       Pointer(*) Variable
Address     ->  Address    -> Value

A variable that is a pointer to a pointer must be declared as,

int **varname;

Example:

#include <stdio.h>

int main () {

   int  a = 0;
   int  *b = NULL;
   int  **c = NULL;

   a = 900;

   b = &a; /* b -> a */
   c = &b;/* c -> b -> a */

   printf("Value of a = %d\n", a );
   printf("Value available at *b = %d\n", *b );
   printf("Value available at **c = %d\n", **c);

   return 0;
}
The output of the above program would be:

Value of a = 900
Value available at *b = 900
Value available at **c = 900



Related topics:
Pointers in C   |   Pointer Arithmetic in C   |   Pointer to an Array in C   |   Returning Array from a Function in C   |   Array of Pointers in C   |   Pointers and Functions in C

List of topics: C Programming

No comments:

Post a Comment