Array of Pointers in C Language with Example Programs

array-of-pointers-in-c-programming-language-with-example-programs

Program Description:

We have looked at the Pointer to an Array in earlier articles. In today’s article, We will look at the Array of Pointers in C programming language with example programs.

An Array of Pointers in C Programming:

As the name suggests, An array of pointers is an Array where every element of the array is a pointer variable.

Unlike the normal array, All elements of the array of pointers are pointer variables, which are capable to store the address of another variable.

Here is the syntax to create the array of pointers in C.

Declaring An Array of Pointers in C:

We can declare an array of pointers by specifying the datatype name followed by the asterisk symbol( *) like below.

data_type *arrOfPointers[SIZE];

Here

  • data_type is the datatype of the pointer variables.
  • * denotes we are creating pointer variables.
  • arrOfPointers is the name of the array of pointers variables.
  • SIZE is the number of elements in the array(i.e Size of an array).

Let’s look at an example.

Example of An Array of Pointer in C:

int *arrPtr[100];

We created an array called arrPtr with size of 100 elements, Where each element is an integer pointer.

Let’s look at an example program to understand the Array of Pointers in C programming language

Program to Understand An Array Of Pointers in C Language:

In the following program, We have created an array of pointers called arrOfPtr with the size 5. So all five elements of the arrOfPtr is an integer pointer.

We also created five variables i, j, k, l, and m and initiated with values 44, 63, 1, 77, and 11 respectively.

Then we stored the address of the above five variables in the arrOfPtr array’s five elements. ( As all elements of the array are pointers).

Finally, We iterated over the array of pointers using a for loop and accessed the values of the above variables through the array of pointer variables.

We used the *arrOfPtr[i](Dereference Operator) to get the value of the variable stored at the ith index of the arrOfPtr array.

Program Output:

Let’s compile and run the program.

array-of-pointers-in-c-program-output

As we can see from the above output, We were able to store the address of the integer variables, and when we dereferenced the elements of the arrOfPtr array we got the integer variables values.

So the array of pointers arrOfPtr is able to store addresses of other variables.

An Array of Pointers with 2D Array in C:

We can use an Array of pointers to access and traverse the 2-dimensional array easily in c programming language.

Each element of the Array of Pointer variable is a pointer variable and the Two-dimensional array is a collection of one-dimensional arrays.

The two-dimensional array elements are stored in the contiguous memory. We can represent a 2-D array as the Matrix.

📢 Learn More about the 2-D Array at – 2D-Array in C – How to Create and Modify Two-dimensional array in C.

How to Store the 2D Array using Array of Pointer Variables:

So we can use an Array of Pointer variables to store the address of a 2D Array. Where each element of the array of pointers will store the address of each row of the 2D-Array.

For example, If you have a 3X3 two-dimensional array( inputArr) i.e Matrix and an Array of three-pointer variables ( arrOfPtr).

int inputArr[3][3];

int *arrOfPtr[3];

Then,

The first element of the array of pointer variables will store the 0th element of the starting row (0th Row – Index starts from zero) of the 2D-Array.

arrOfPtr[0] will store the inputArr[0]

Similarly, 1st index pointer in the array of pointers stores the 1st index row of the matrix.

arrOfPtr[1] will store the inputArr[1]

and

arrOfPtr[2] will store the inputArr[2]

This way we can store the 2D-Array or Matrix with an Array of Pointers variable.

How to Get 2D-Array Elements using the Array of Pointer variable:

We can access the element so the 2D-Array through the array of pointer variables by dereferencing the array of pointer variables arrOfPtr Twice.

For Example to get the value at the row index i and column index j of the 2D Array, We can dereference twice the array of pointer variables like below.

arrOfPtr[i][j]

Let’s plug everything together and create a program that demonstrates how to store the 2D array using an array of pointers and how to get the element values of the 2D Array using the array of pointer variables.

Program Output:

Compile the program.

$ gcc array-of-pointers-2d-array.c

The above command generates the executable file named a.out. Run the executable file.

array-of-pointers-with-2D-Array-in-C-Example-program

As we can see from the above output, We are able to store the 2D-array using an array of pointer variables and We are also able to dereference twice and get the values of the 2D-Array using the Array of pointer variables.

Related Pointers Tutorials and Programs:

Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

1 Response

  1. […] Array of Pointers in C Language with Example Programs […]

Leave a Reply