YouTip LogoYouTip

C Array Of Pointers

## Introduction to C Array of Pointers In C programming, an **array of pointers** is an array where each element is a pointer pointing to a specific data type. Instead of storing raw data values directly, the array stores memory addresses that point to other data objects. An array of pointers is highly versatile. It allows you to: * Store a collection of pointers pointing to different variables. * Manage dynamically allocated memory blocks. * Handle arrays of strings (character arrays) of varying lengths efficiently. * Work with complex data structures like arrays of structures. --- ## Syntax and Declaration To declare an array of pointers, use the following syntax: ```c data_type *array_name; ``` For example: ```c int *ptr; ``` Here, `ptr` is declared as an array of `3` pointers, where each element is a pointer to an `int` value. ### Understanding the Operator Precedence In the declaration `int *ptr`, the subscript operator `[]` has higher precedence than the dereference operator `*`. Therefore, `ptr` is first associated with `` (making it an array of size 3), and then with `*` (making its elements pointers to `int`). --- ## Code Examples and Practical Use Cases ### Example 1: Basic Array of Integer Pointers Let's look at a simple example where we declare an array of pointers, assign the addresses of individual integer variables to its elements, and then access the values using dereferencing. ```c #include int main() { int num1 = 10, num2 = 20, num3 = 30; // Declare an array of 3 integer pointers int *ptrArray; // Assign the addresses of integer variables to the pointer array ptrArray = &num1; ptrArray = &num2; ptrArray = &num3; // Access and print the values using the pointer array printf("Value at index 0: %d\n", *ptrArray); printf("Value at index 1: %d\n", *ptrArray); printf("Value at index 2: %d\n", *ptrArray); return 0; } ``` **Output:** ```text Value at index 0: 10 Value at index 1: 20 Value at index 2: 30 ``` --- ### Example 2: Mapping an Array of Pointers to an Existing Array You can also map the elements of a pointer array to the elements of a standard array. This is useful when you want to manipulate or sort references to array elements without modifying the original array directly. ```c #include const int MAX = 3; int main() { int var[] = {10, 100, 200}; int i; int *ptr; // Assign the address of each element of 'var' to the pointer array 'ptr' for (i = 0; i < MAX; i++) { ptr = &var; } // Access the values using the pointer array for (i = 0; i < MAX; i++) { printf("Value of var[%d] = %d\n", i, *ptr); } return 0; } ``` **Output:** ```text Value of var = 10 Value of var = 100 Value of var = 200 ``` --- ### Example 3: Array of Pointers to Strings (Character Pointers) One of the most common applications of pointer arrays in C is creating an array of strings. Since a string in C is represented as a null-terminated character array (`char*`), an array of strings is represented as an array of character pointers (`char *arr[]`). This approach is highly memory-efficient because it creates a **ragged array** (or jagged array), where each string can have a different length, consuming only the exact amount of memory required. ```c #include const int MAX = 4; int main() { // Declare an array of character pointers pointing to string literals const char *names[] = { "Zara Ali", "Hina Ali", "Nuha Ali", "Sara Ali" }; int i = 0; // Loop through and print each string for (i = 0; i < MAX; i++) { printf("Value of names[%d] = %s\n", i, names); } return 0; } ``` **Output:** ```text Value of names = Zara Ali Value of names = Hina Ali Value of names = Nuha Ali Value of names = Sara Ali ``` --- ## Key Considerations and Best Practices When working with arrays of pointers in C, keep the following points in mind: 1. **Pointer vs. Array Precedence:** * `int *ptr` is an **array of pointers** (size `MAX`, elements are `int*`). * `int (*ptr)` is a **pointer to an array** of `MAX` integers. The parentheses change the meaning completely. 2. **Memory Management:** If you dynamically allocate memory for elements in a pointer array (e.g., using `malloc`), you must free the memory allocated for each individual pointer before freeing the array itself (if the array was also dynamically allocated) to prevent memory leaks. 3. **Const Correctness:** When pointing to string literals (as in Example 3), always use `const char *` instead of `char *`. String literals are stored in read-only memory, and attempting to modify them via a pointer will result in undefined behavior (usually a segmentation fault). 4. **Null Pointers:** It is a good practice to initialize unused pointer array elements to `NULL` to avoid wild pointers and make debugging easier.
← C Pointer To PointerC Pointer Arithmetic β†’