Thursday, March 2, 2017

Array of Pointers

To store many pointer variables, one can use array of pointers.

#include <stdio.h>
int main () {

   int  number[5], i, *ptr[5];

   for(i=0; i<5; i++)
   {
     printf("Enter number[%d]:",i);
     scanf("%d",&number[i]);
   }
   /* Loop to assign the address of integer into pointer array */
   for ( i = 0; i < 5; i++) {
      ptr[i] = &number[i]; 
   }
   printf("Value printed using Pointer array.\n");
   for ( i = 0; i < 5; i++) {
      printf("Value of number[%d] = %d\n", i, *ptr[i] );
   }
   return 0;
}

Output of program:

Enter number[0]:2
Enter number[1]:4
Enter number[2]:6
Enter number[3]:8
Enter number[4]:10
Value printed using Pointer array.
Value of number[0] = 2
Value of number[1] = 4
Value of number[2] = 6
Value of number[3] = 8
Value of number[4] = 10

No comments:

Post a Comment