• C Programming Video Tutorials

Array of Pointers in C



What is an Array of Pointers?

Just like an integer array holds a collection of integer variables, an "array of pointers" would hold variables of pointer type. It means each variable in an array of pointers is a pointer that points to another address.

The name of an array can be used as a pointer because it holds the address to the first element of the array. If we store the address of an array in another pointer, then it is possible to manipulate the array using pointer arithmetic.

Let's suppose we have an array called tpdoc and we access its values as tpdoc[i] where "i" is the index value. There is another way you can access the array elements, i.e., using the pointers property of the array.

You can use "*(tpdoc + i)" instead of "tpdoc[i]". Here, we are using the dereferencing operator (*) on the array "tpdoc" to get the value from the address "(tpdoc + i)".

Let's start with a simple example of an array of integers before moving onto how an array of pointers works.

Example: A Simple Array of Integers

Take a look at the the following example. It uses an array of 3 integers −

#include <stdio.h>
 
const int MAX = 3;
 
int main(){

   int var[] = {10, 100, 200};
   int i;
 
   for(i = 0; i < MAX; i++){
      printf("Value of var[%d] = %d\n", i, var[i]);
   }
   
   return 0;
}

Output

When the above code is compiled and executed, it produces the following result −

Value of var[0] = 10
Value of var[1] = 100
Value of var[2] = 200

There may be a situation when we want to maintain an array that can store pointers to an "int" or "char" or any other data type available.

An Array of Pointers to Integers

Here is the declaration of an array of pointers to an integer −

int *ptr[MAX];

It declares ptr as an array of MAX integer pointers. Thus, each element in ptr holds a pointer to an int value.

Example

The following example uses three integers, which are stored in an array of pointers, as follows −

#include <stdio.h>
 
const int MAX = 3;
 
int main(){

   int var[] = {10, 100, 200};
   int i, *ptr[MAX];
 
   for(i = 0; i < MAX; i++){
      ptr[i] = &var[i]; /* assign the address of integer. */
   }
   
   for (i = 0; i < MAX; i++){
      printf("Value of var[%d] = %d\n", i, *ptr[i]);
   }
   
   return 0;
}

Output

When the above code is compiled and executed, it produces the following result −

Value of var[0] = 10
Value of var[1] = 100
Value of var[2] = 200

An Array of Pointers to Characters

You can also use an array of pointers to character to store a list of strings as follows −

#include <stdio.h>
 
const int MAX = 4;
 
int main(){

   char *names[] = {
      "Zara Ali",
      "Hina Ali",
      "Nuha Ali",
      "Sara Ali"
   };
   
   int i = 0;

   for(i = 0; i < MAX; i++){
      printf("Value of names[%d] = %s\n", i, names[i]);
   }
   
   return 0;
}

Output

When the above code is compiled and executed, it produces the following result −

Value of names[0] = Zara Ali
Value of names[1] = Hina Ali
Value of names[2] = Nuha Ali
Value of names[3] = Sara Ali
Advertisements