Download the BYJU'S Exam Prep App for free GATE/ESE preparation videos & tests - Download the BYJU'S Exam Prep App for free GATE/ESE preparation videos & tests -

An Array of Pointers in C

When we require multiple pointers in a C program, we create an array of multiple pointers and use it in the program. We do the same for all the other data types in the C program.

In this article, we will take a look at the Array of Pointers in C and its uses according to the GATE Syllabus for CSE (Computer Science Engineering). Read ahead to learn more.

Table of Contents

An Array of Pointers in C

The pointers and arrays are very closely related to one another in the C language. The program considers an array as a pointer. In simpler words, an array name consists of the addresses of the elements. Before we understand what an array of pointers is, let us understand more about pointers and arrays separately.

What is an Array?

If we want to include multiple elements in a program with the very same data type, we can use an array. Let us assume that we are using a total of five integers in any program. There are two different ways in which we can allocate memory to all of these integers:

  • We can create five integers differently;
  • We can create an array of all the different integers.

One of the huge advantages of using arrays is that it becomes very easy for a programmer to access all the elements in the program easily. All the elements can be accessed in a single run of a loop in the program.

What is a Pointer?

The pointers in C point towards the variables present in a program. They hold the addresses of the variables. The program will use these addresses to access the variables and modify them. Here are the three crucial things that come into play when dealing with pointers in C:

  • Declaration of the pointer variable
  • Initialisation of the pointer variable
  • Using the pointer to access the variable

What is an Array of Pointers in C?

When we want to point at multiple variables or memories of the same data type in a C program, we use an array of pointers.

Let us assume that a total of five employees are working at a cheesecake factory. We can store the employees’ names in the form of an array. Along with this, we also store the names of employees working at the office, the library, and the shoe store.

Now, let us assume that once we read all the names of the employees randomly, we want to sort all of the employees’ names working in the cheesecake factory. Sorting all the employee names requires reading, swapping, and copying a lot of data. But since we have stored all the employee names in groups/ arrays, we can directly refer to the array of names of employees working at the cheesecake factory.

The Application of Arrays of Pointers

Let us assume that we want to build an enclosed system, and this system uses a different kind of thermometer for measuring the temperature. Now, in this case, we can use an array for holding the address of memory for every sensor. This way, manipulation of the sensor status becomes very easy.

Example

The thermo[0] will be holding the 1st sensor’s address.

The thermo[1] will be holding the 2nd sensor’s address and so on.

Now, since it’s an array, it can interact directly with the thermo pointer indexed in the array. Thus, we will get the 1st sensor’s status with the thermo[0], the second one using the thermo[1], and so on ahead.

Declaration of an Array of Pointers in C

An array of pointers can be declared just like we declare the arrays of char, float, int, etc. The syntax for declaring an array of pointers would be:

data_type *name_of_array [array_size];

Now, let us take a look at an example for the same,

int *ary[55]

This one is an array of a total of 55 pointers. In simple words, this array is capable of holding the addresses a total of 55 integer variables. Think of it like this- the ary[0] will hold the address of one integer variable, then the ary[1] will hold the address of the other integer variable, and so on.

Example

Let us take a look at a program that demonstrates how one can use an array of pointers in C:

#include<stdio.h>

#define SIZE 10

int main()

{

int *arr[3];

int p = 40, q = 60, r = 90, i;

arr[0] = &p;

arr[1] = &q;

arr[2] = &r;

for(i = 0; i < 3; i++)

{

printf(“For the Address = %d\t the Value would be = %d\n”, arr[i], *arr[i]);

}

return 0;

}

The output generated out of the program mentioned above would be like this:

For the Address = 387130656 the Value would be = 40

For the Address = 387130660 the Value would be = 60

For the Address = 387130664 the Value would be = 90

How Does it Work?

Take a look at how we assigned the addresses of the pointers p, q, and r. We first assign the p variable’s address to the 0th element present in the array. In a similar manner, the 1st and 2nd elements of the array will be assigned with the addresses of q and r. At this very point, the array, arr would look like this:

The arr[i] provides the address of the array’s ith element. Thus, the arr[0] would return the address of the variable p, the arr[1] would return the address of the pointer q, and so on. We use the * indirection operator to get the value present at the address. Thus, it would be like this:

*arr[i]

Thus, *arr[0] would generate the value at the arr[0] address. In a similar manner, the *arr[1] would generate the value at the arr[1] address, and so on.

Practice Problems on Array of Pointers in C

1. Take a look at the following program in C and find out the output for the same:

#include <stdio.h>

const int MAX = 6;

int main () {

char *vegetables[] = {

“Broccoli”,

“Carrot”,

“Eggplant”,

“Spinach”,

“Asparagus”,

“Zucchini”

};

int a = 0;

for ( a = 0; a < MAX; a++) {

printf(“The name of the vegetable[%d] = %s\n”, a, names[a] );

}

return 0;

}

Answer: The name of the vegetable[0] = Broccoli

The name of the vegetable[1] = Carrot

The name of the vegetable[2] = Eggplant

The name of the vegetable[3] = Spinach

The name of the vegetable[4] = Asparagus

The name of the vegetable[5] = Zucchini

2. Take a look at the following program in C and find out the output for the same:

#include <stdio.h>

const int MAX = 5;

int main () {

int num[] = {10, 20, 30, 40, 50, 60, 70};

int x, *ptr[MAX];

for ( x = 0; x < MAX; x++) {

ptr[x] = &num[x];

}

for ( x = 0; x < MAX; x++) {

printf(“The value of the num[%d] = %d\n”, x, *ptr[x] );

}

return 0;

}

Answer: The value of the num[0] = 10

The value of the num[1] = 20

The value of the num[2] = 30

The value of the num[3] = 40

The value of the num[4] = 50

The value of the num[5] = 60

The value of the num[6] = 70


FAQs

Q1

Can we create an array of pointers in C?

Yes, we can. When we require multiple pointers in a C program, we create an array of multiple pointers and use it in the program. We do the same for all the other data types in the C program.

Q2

What is the advantage of using an array of pointers in C?

When we want to point at multiple variables or memories of the same data type in a C program, we use an array of pointers.

One of the huge advantages of using arrays is that it becomes very easy for a programmer to access all the elements in the program easily. All the elements can be accessed in a single run of a loop in the program.

Let us assume that we want to build an enclosed system, and this system uses a different kind of thermometer for measuring the temperature. Now, in this case, we can use an array for holding the address of memory for every sensor. This way, manipulation of the sensor status becomes very easy.

Q3

What is a pointer of pointer in C?

It is basically a type of multiple indirections in a program. The pointer of a pointer is like a chain of pointers. Now, a pointer usually consists of a variable’s address. But on the other hand, when we define the pointer to a pointer, then the first pointer consists of the second pointer’s address. Now, the second pointer points towards the location of the actual value in the program.

Keep learning and stay tuned to get the latest updates on GATE Exam along with GATE Eligibility Criteria, GATE 2023, GATE Admit Card, GATE Syllabus for CSE (Computer Science Engineering), GATE CSE Notes, GATE CSE Question Paper, and more.

Also Explore,

Comments

Leave a Comment

Your Mobile number and Email id will not be published.

*

*