The array of pointers is a special array where each index contains an address rather than values unlike a normal array.
Syntax:
data_type *array_name [size_of_array];
Example:
char *str[3]= {"tech", "access", "blogging" };
Thus here the “str” is an array of character pointers.
Please refer to the topics below for a clear understanding before reading further:
- Array in C
- Introduction to pointer
- Pointer declaration, initialization, and dereferencing
- Pointer to array
Sample Program:
Array of pointers to integer
#include <stdio.h>
int main()
{
int a = 10, b = 20, c =30, d = 40, e =50, i;
int *arr[5];
arr[0] = &a;
arr[1] = &b;
arr[2] = &c;
arr[3] = &d;
arr[4] = &e;
printf ("\n Address of a is: %p\n", arr[0]);
printf ("\n Address of b is: %p\n", arr[1]);
printf ("\n Address of c is: %p\n", arr[2]);
printf ("\n Address of d is: %p\n", arr[3]);
printf ("\n Address of e is: %p\n", arr[4]);
/* Values stored at above address */
for (i = 0; i <5; i++)
printf ("\n Value stored at arr[%d] = %d\n", i, *arr[i]);
return 0;
}
Output:
Address of a is: 0x7ffddf47f788
Address of b is: 0x7ffddf47f78c
Address of c is: 0x7ffddf47f790
Address of d is: 0x7ffddf47f794
Address of e is: 0x7ffddf47f798
Value stored at arr[0] = 10
Value stored at arr[1] = 20
Value stored at arr[2] = 30
Value stored at arr[3] = 40
Value stored at arr[4] = 50

Where arr[0] holding the address of the variable a. i.e arr[0] = 1024 and *arr[0]-> *1024-> value stored at the memory address 1024 is 10. So *arr[0] is 10. Similarly, *arr[1] = 20 and so on.
Sample Program2:
Array of pointers to string:
This example is appropriate to understand the importance of an array of pointers.
#include <stdio.h>
int main()
{
char *str[3] = {"tech", "access", "blogger"};
printf("\n 1st string(str[0]) is: %s\n", str[0]);
printf("\n 2nd string(str[1]) is: %s\n", str[1]);
printf("\n 3rd string(str[2]) is: %s\n", str[2]);
printf("\n The third character of %s is: %c", str[0], *(str[0] + 2));
printf("\n The fourth character of %s is: %c", str[1] , *(str[1] + 3));
printf("\n The base address of %s is %p", str[0], str[0]);
printf("\n The address of %s 1st character %c is: %p", str[0], *(str[0] + 1), str[0] + 1);
return 0;
}
Output:
1st string(str[0]) is: tech
2nd string(str[1]) is: access
3rd string(str[2]) is: blogger
The third character of tech is: c
The fourth character of access is: e
The base address of tech is: 0x4006b8
The address of tech 1st character e is 0x4006b9
Memory Representation:

Array of pointer to structure:
Array of pointers can also be used to keep the address of structure at each index of an array
Example:
#include <stdio.h>
struct Student
{
char name[10];
int roll;
float marks;
};
int main()
{
struct Student *stud[3];
stud[0] =(struct Student *)malloc(sizeof(struct Student));
stud[0]->roll =5;
strcpy(stud[0]->name, "tech");
stud[0]->marks =8.8;
printf ("\n Name is: %s\n", stud[0]->name);
printf ("\n Roll number is: %d\n", stud[0]->roll);
printf ("\n Marks is: %f\n", stud[0]->marks);
return 0;
}
Output:
Name is: tech
Roll number is: 5
Marks is: 8.800000
Importance of an array of pointers:
The above program can also be implemented with the help of 2-D array
char *str[3] = {"tech", "access", "info"};
char str1[3][10]= {"tech", "access", "info"};
Sample Program:
#include <stdio.h>
int main()
{
char *str[3] = {"tech", "access", "blogger"};
char str1[3][10] = {"tech", "access", "blogger"};
printf ("\n Size with pointer is: %lu\n", sizeof(str));
printf ("\n Size without pointer is: %lu\n", sizeof(str1));
return 0;
}
Output:
Size with pointer is: 24
Size without pointer: 30
The above program clearly shows that the memory is saved with a pointer approach as we are using 3 pointers each of size 8. The size of the pointer variable differs from compiler to compiler
Application of an array of pointers:
Assuming that we are going to build an embedded system that uses different kinds of system counters to count packets
In this case, we can use an array of pointers to hold the memory address of each type of counters so that it will be very easy to manipulate the sensor status.
Example
counter[0] will hold the address of the 32 bit counters.
counter[1] will hold the address of the 64 bit counters and so on.
Since it is an array, we can directly interact with the particular counter using array index.
We can get the counter status of 32 bit using counter[0], 64 bit counter status using counter1], and so on.
Relevant Topics:
- Array in C
- Introduction to pointer
- Pointer declaration, initialization, and dereferencing
- Pointer to array
Categories: C Language
Leave a Reply