Just like we can declare an array of
char etc, we can also declare an array of pointers, here is the syntax to do the same.
Let’s take an example:
arrop is an array of
5 integer pointers. It means that this array can hold the address of
5 integer variables. In other words, you can assign
5 pointer variables of type pointer to
int to the elements of this array.
The following program demonstrates how to use an array of pointers.
#define SIZE 10
int a = 10, b = 20, c = 50, i;
arrop = &a;
arrop = &b;
arrop = &c;
for(i = 0; i < 3; i++)
printf("Address = %d\t Value = %d\n", arrop[i], *arrop[i]);
Address = 387130656 Value = 10
Address = 387130660 Value = 20
Address = 387130664 Value = 50
How it works:
Notice how we are assigning the addresses of
c. In line 9, we are assigning the address of variable
a to the 0th element of the of the array. Similarly, the address of
c is assigned to 1st and 2nd element respectively. At this point, the
arrop looks something like this:
arrop[i] gives the address of ith element of the array. So
arrop returns address of variable
arrop returns address of
b and so on. To get the value at address use indirection operator (
*arrop gives value at address
*arrop gives the value at address
arrop and so on.