Array of pointers

Updated: 04/26/2017 by Computer Hope

array of pointersIn computer programming, an array of pointers is an indexed set of variables in which the variables are pointers (a reference to a location in memory).

Pointers are an important tool in computer science for creating, using, and destroying all types of data structures. An array of pointers is useful for the same reason that all arrays are useful: it allows you to numerically index a large set of variables.

Below is an array of pointers in C: it sets each pointer in one array to point to an integer in another and then prints the values of the integers by dereferencing the pointers (printing the value in memory that the pointers point to).

#include <stdio.h>
const int ARRAY_SIZE = 5;    
int main ()
   /* first, declare and set an array of five integers:    */ 
   int array_of_integers[] = {5, 10, 20, 40, 80};  

   /* next, declare an array of five pointers-to-integers: */
   int i, *array_of_pointers[ARRAY_SIZE]; 
   for ( i = 0; i < ARRAY_SIZE; i++)
      /* for indices 1 through 5, set a pointer to 
         point to a corresponding integer:                 */
      array_of_pointers[i] = &array_of_integers[i];    

   for ( i = 0; i < ARRAY_SIZE; i++)
      /* print the values of the integers pointed to 
         by the pointers:                                  */
      printf("array_of_integers[%d] = %d\n", i, *array_of_pointers[i] );

   return 0;

The output of the above program is:

array_of_integers[0] = 5 
array_of_integers[1] = 10 
array_of_integers[2] = 20 
array_of_integers[3] = 40 
array_of_integers[4] = 80 

Array, Computer Science, Memory, Pointer, Programming terms