Arrays of Pointers in C: A Comprehensive Guide
Delve into the concept of arrays of pointers in C, a powerful tool for managing and accessing data indirectly.
Array of Pointers in C
What is an Array of Pointers?
An array of pointers is similar to an integer array, but instead of holding integer values, it contains pointers. Each element in this array points to another address, effectively allowing you to store references to other variables.
The name of the array itself acts as a pointer to the first element. You can manipulate the array using pointer arithmetic if you store its address in another pointer.
Creating an Array of Pointers
To declare an array of pointers in C, you use a syntax similar to that of a regular pointer. Start with the data type, follow it with an asterisk (*), and then specify the name of the array with the size in square brackets ([]).
Each element in the array of pointers holds the address of a variable of a specific type.
Example of Creating an Array of Pointers
Below is an example demonstrating how to create and use an array of pointers. We declare three integer variables and use an array of pointers to access and print their values.
Syntax
#include
int main() {
// Declaring integers
int var1 = 5;
int var2 = 10;
int var3 = 15;
// Declaring an array of pointers to integers
int *ptr[3];
// Initializing each element of
// array of pointers with the addresses of
// integer variables
ptr[0] = &var1;
ptr[1] = &var2;
ptr[2] = &var3;
// Accessing values
for (int i = 0; i < 3; i++) {
printf("Value at ptr[%d] = %d\n", i, *ptr[i]);
}
return 0;
}
Output
Value at ptr[0] = 5
Value at ptr[1] = 10
Value at ptr[2] = 15
An Array of Pointers to Integers
You can declare an array of pointers to integers like this:
int *ptr[MAX];
This creates an array called ptr that can hold pointers to integer values.
Example
The following example uses three integers stored in an array of pointers:
Syntax
#include
const int MAX = 3;
int main() {
int var[] = {20, 40, 60};
int i, *ptr[MAX];
for(i = 0; i < MAX; i++) {
ptr[i] = &var[i]; // Assigning the address of each integer
}
for (i = 0; i < MAX; i++) {
printf("Value of var[%d] = %d\n", i, *ptr[i]);
}
return 0;
}
Output
Value of var[0] = 20
Value of var[1] = 40
Value of var[2] = 60
An Array of Pointers to Characters
You can also create an array of pointers to characters to store a list of strings, as shown below:
Syntax
#include
const int MAX = 4;
int main() {
char *names[] = {
"John Doe",
"Jane Smith",
"Emily Johnson",
"Michael Brown"
};
for(int i = 0; i < MAX; i++) {
printf("Value of names[%d] = %s\n", i, names[i]);
}
return 0;
}
Output
Value of names[0] = John Doe
Value of names[1] = Jane Smith
Value of names[2] = Emily Johnson
Value of names[3] = Michael Brown
An Array of Pointers to Structures
If you want to manage a list of structures using pointers, you can declare an array of structure pointers. Here's an example:
Syntax
#include
#include
#include
// Declaring a structure
typedef struct {
char title[50];
float price;
} Book;
const int MAX = 3;
int main() {
Book *book[MAX];
// Initialize each book (pointer)
for (int i = 0; i < MAX; i++) {
book[i] = malloc(sizeof(Book));
snprintf(book[i]->title, 50, "Novel %d", i + 1);
book[i]->price = 50 + i * 10;
}
// Print details of each book
for (int i = 0; i < MAX; i++) {
printf("Title: %s, Price: %.2f\n", book[i]->title, book[i]->price);
}
// Free allocated memory
for (int i = 0; i < MAX; i++) {
free(book[i]);
}
return 0;
}
Output
Title: Novel 1, Price: 50.00
Title: Novel 2, Price: 60.00
Title: Novel 3, Price: 70.00