Friday, July 29, 2016

C Basics - Array of Pointers in C

Following is the declaration of an array of pointers to an integer –

int *ptr[3];

It declares ptr as an array of 3 integer pointers. Thus, each element in ptr, holds a pointer to an int value.

#include <stdio.h>

int main () {

   int  a = 10, b = 20, c = 30;
   int *iptr[3]; /*array of integer pointers*/
   
   iptr[0] = &a;
   iptr[1] = &b;
   iptr[2] = &c;   
 
   printf("Value of a = %d\n", *iptr[0] );
   printf("Value of b = %d\n", *iptr[1] );
   printf("Value of c = %d\n", *iptr[2] );   
   
   return 0;
}
The output of the above program would be:

Value of a = 10
Value of b = 20
Value of c = 30



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

List of topics: C Programming

No comments:

Post a Comment