Array of Pointers in C

Just like we can declare an array of int, float or char etc, we can also declare an array of pointers, here is the syntax to do the same.

Syntax: datatype *array_name[size];

Let’s take an example:

Here 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.

Expected Output:

How it works:

Notice how we are assigning the addresses of a, b and 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 b and c is assigned to 1st and 2nd element respectively. At this point, the arrop looks something like this:

array of pointers

arrop[i] gives the address of ith element of the array. So arrop[0] returns address of variable a, arrop[1] returns address of b and so on. To get the value at address use indirection operator (*).

So, *arrop[0] gives value at address arrop[0], Similarly *arrop[1] gives the value at address arrop[1] and so on.

7 thoughts on “Array of Pointers in C

  1. Shouldn’t the addresses in the arrop array diagram be consecutive? In other words, the address of a is 2000. Therefore, the address of b should be 2004 and the address of c should be 2008. This is because in this example ints are presumably 4 bytes, and the elements in an array are consecutive.

Leave a Comment

%d bloggers like this: