What is an Array of Pointers in C?
An Array of Pointers is an array that stores the addresses (pointers) of other variables rather than storing actual values. Each element of this array is a pointer, typically used to point to a variable or the first element of another array or string.
Syntax and Declaration
To declare an array of pointers, write the data type followed by an asterisk (*), then the array name with its size:
data_type *arrayName[size];
Example:
int *arr[5]; // Array of 5 pointers to integers
In this example, arr is an array that holds five pointers. Each pointer in the array can store the address of an integer.
Initializing an Array of Pointers
Static Initialization: You can initialize an array of pointers at the time of declaration.
int a = 10, b = 20, c = 30; int *arr[3] = {&a, &b, &c};
In this example, each element of arr holds the address of an integer variable.
Dynamic Initialization: For dynamic memory allocation, use malloc to assign memory during runtime.
int *arr[3]; for (int i = 0; i < 3; i++) { arr[i] = (int *)malloc(sizeof(int)); *arr[i] = i * 10; }
This approach allocates memory for each pointer in the array and assigns values dynamically.
Accessing Elements
Accessing values through an array of pointers involves dereferencing each pointer:​
for (int i = 0; i < 3; i++) { printf("%d\n", *arr[i]); }
This loop prints the values pointed to by each element in the array.
Practical Examples
Example 1: Array of Pointers to Strings (Storing Multiple Strings)
In this example, we declare an array of pointers to store multiple strings. Each element in the array holds a pointer to the first character of each string.
#include <stdio.h> int main() { // Array of pointers to store multiple strings char *arr[] = {"Hello", "World", "C Programming"}; // Printing each string using the array of pointers for (int i = 0; i < 3; i++) { printf("%s\n", arr[i]); } return 0; }
Output:
Hello World C Programming
This program uses an array of pointers to store three strings: “Hello”, “World”, and “C Programming”. A loop prints each string using these pointers. This is a simple and efficient way to handle multiple strings in C.
Example 2: Array of Pointers to Integers (Accessing Different Integer Variables)
In this example, we declare an array of pointers to integers and initialize it with the addresses of different integer variables. The program then uses the pointers to access and print the values.
#include <stdio.h> int main() { // Declare integer variables with different names int num1 = 100, num2 = 200, num3 = 300; // Array of pointers to integers int *arr[3] = {&num1, &num2, &num3}; // Accessing and printing the values using the array of pointers for (int i = 0; i < 3; i++) { printf("Value at arr[%d] = %d\n", i, *arr[i]); } return 0; }
Output:
Value at arr[0] = 100 Value at arr[1] = 200 Value at arr[2] = 300
This program creates three integer variables: 100, 200, and 300. It stores their addresses in an array of pointers. A loop goes through the array and prints each value using the pointers. This shows how you can use pointer arrays to access and print multiple variables easily.
Example 3: Array of Pointers to Strings
This is one of the most common uses of pointer arrays—handling multiple strings efficiently.
#include <stdio.h> int main() { const char *students[] = {"John", "Emily", "Sophia"}; for (int i = 0; i < 3; i++) { printf("Student[%d]: %s\n", i, students[i]); } return 0; }
Output:
Student[0]: John Student[1]: Emily Student[2]: Sophia
This program creates an array of string pointers, students, that holds the names of three students. Using a loop, it prints each student’s name with its corresponding index in the array. The program demonstrates how to store and access multiple strings through an array of pointers.
Best Practices
- Memory Management: Always free dynamically allocated memory to prevent memory leaks:​
- Initialization: Ensure pointers are initialized before use to avoid undefined behavior.​
- Bounds Checking: Always perform bounds checking when accessing array elements to prevent segmentation faults.
for (int i = 0; i < rows; i++) { free(matrix[i]); } free(matrix);
Sanfoundry Global Education & Learning Series – 1000 C Tutorials.
- Check Computer Science Books
- Practice Computer Science MCQs
- Check C Books
- Practice BCA MCQs
- Apply for Computer Science Internship