An array of pointers is an array where each element is a pointer to another variable or an array. This concept is useful for creating arrays that can store addresses of different data types or arrays, allowing for flexible and dynamic data structures.
An array of pointers can be declared by specifying the type of data the pointers will point to, followed by an asterisk (*), and then the array name with square brackets indicating the size of the array.
#include <stdio.h>
int main() {
int a = 10, b = 20, c = 30;
int *arr[3]; // Array of 3 integer pointers
arr[0] = &a; // Pointing to variable a
arr[1] = &b; // Pointing to variable b
arr[2] = &c; // Pointing to variable c
for (int i = 0; i < 3; i++) {
printf("Value at arr[%d] = %d\n", i, *arr[i]);
}
return 0;
}An array of pointers is often used to create an array of strings, where each element of the array points to the first character of a string.
#include <stdio.h>
int main() {
char *arr[] = {
"Hello",
"World",
"C Programming"
};
for (int i = 0; i < 3; i++) {
printf("String at arr[%d] = %s\n", i, arr[i]);
}
return 0;
}Understanding arrays of pointers is essential for dynamic memory management, creating flexible data structures, and handling complex data efficiently in C programming.