๐งฎ C Array of Pointers โ Flexible and Dynamic Data Storage in C
๐งฒ Introduction โ What Is an Array of Pointers in C?
In C programming, an array of pointers is a collection of pointers, where each element of the array is itself a pointer. This structure provides flexibility to store addresses of different data elements such as strings, arrays, or dynamically allocated memory blocks.
๐ฏ In this guide, youโll learn:
- How to declare and initialize arrays of pointers
- Use cases like arrays of strings and arrays of arrays
- Accessing and manipulating values via pointer arrays
- Best practices and common pitfalls
๐ Core Concept โ Array Holding Pointers
A regular array holds actual values of the same type. An array of pointers holds addresses, pointing to data stored elsewhere in memory.
โ Syntax:
int *arr[5]; // Array of 5 pointers to int
char *strs[3]; // Array of 3 pointers to char (strings)
๐ป Code Examples โ Array of Pointers in Action
โ Example 1: Array of Pointers to Integers
#include <stdio.h>
int main() {
int a = 10, b = 20, c = 30;
int *arr[3] = {&a, &b, &c};
for (int i = 0; i < 3; i++) {
printf("Value at arr[%d] = %d\n", i, *arr[i]);
}
return 0;
}
๐จ๏ธ Output:
Value at arr[0] = 10
Value at arr[1] = 20
Value at arr[2] = 30
โ Example 2: Array of Pointers to Strings (Array of Strings)
#include <stdio.h>
int main() {
char *names[] = {"Alice", "Bob", "Charlie"};
for (int i = 0; i < 3; i++) {
printf("Name[%d]: %s\n", i, names[i]);
}
return 0;
}
๐จ๏ธ Output:
Name[0]: Alice
Name[1]: Bob
Name[2]: Charlie
๐ Each names[i]
points to the first character of a string.
โ Example 3: Array of Pointers with Dynamic Memory Allocation
#include <stdio.h>
#include <stdlib.h>
int main() {
int *arr[3];
for (int i = 0; i < 3; i++) {
arr[i] = (int *)malloc(sizeof(int));
*arr[i] = i * 10;
}
for (int i = 0; i < 3; i++) {
printf("Value at arr[%d] = %d\n", i, *arr[i]);
free(arr[i]);
}
return 0;
}
๐ Demonstrates dynamic allocation of integers pointed to by an array.
๐ Use Cases for Array of Pointers
Use Case | Description |
---|---|
String table | Storing an array of variable-length strings |
Dynamic arrays | Point to different memory blocks |
Function tables | Array of function pointers |
Matrix (2D array) simulation | Array of pointers to arrays (rows) |
๐ก Best Practices & Tips
๐ Best Practice:
Always ensure pointers in the array are initialized before use.
๐ก Tip:
Use char *arr[]
for arrays of strings instead of char arr[][]
to support variable-length strings.
โ ๏ธ Pitfall:
Accessing uninitialized pointers can cause segmentation faults.
๐ ๏ธ Real-World Applications
- ๐งพ Menu systems with string arrays
- ๐งช Command-line arguments (
char *argv[]
) - ๐งฎ Dynamic 2D arrays for scientific computing
- ๐งฉ Memory-efficient data tables and buffers
๐ Summary โ Recap & Next Steps
Arrays of pointers offer flexibility and dynamic memory control. They are widely used in handling strings, function lists, and custom data structures.
๐ Key Takeaways:
- Array of pointers stores addresses, not raw values
- Great for variable-length strings and dynamic memory
- Use
*arr[i]
to access the value pointed by each element - Useful for creating flexible, memory-efficient applications
โ๏ธ Real-World Relevance:
Used in shell argument parsing, language interpreters, embedded UI menus, and matrix-heavy applications.
โ Frequently Asked Questions (FAQ)
โ What is an array of pointers?
โ
It’s an array where each element is a pointer to a data value, like int *arr[5];
.
โ How is char *arr[]
different from char arr[][]
?
โ
char *arr[]
allows variable-length strings. char arr[][]
requires fixed-length strings per row.
โ Can I use array of pointers for 2D arrays?
โ Yes. You can simulate a matrix by making each pointer in the array point to a row.
โ Can I assign addresses to an array of pointers?
โ
Yes. Example: arr[0] = &value;
, or arr[1] = malloc(sizeof(int));
โ Is argv
in main(int argc, char *argv[])
an array of pointers?
โ
Yes. argv
is an array of character pointers (each pointing to a command-line string).
Share Now :