In computer programming, an array of pointers is an indexed set of variables, where the variables are pointers (referencing 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 lets you numerically index a large set of variables.
Below is an array of pointers in C that points each pointer in one array to an integer in another array. The value of each integer is printed by dereferencing the pointers. In other words, this code prints the value in memory of where the pointers point.
#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
Related Pages
- How to create a computer program.