top of page

Array of pointers

  • Writer: Siddharth Sharma
    Siddharth Sharma
  • Nov 8, 2024
  • 2 min read

An array of pointers is an array where each element is a pointer. This structure is helpful when working with variable-length data like strings or arrays, or when dynamically allocating memory for multiple items. In C and C++, an array of pointers is commonly used to manage lists of strings, 2D arrays, and arrays of dynamically allocated memory.


Declaring an Array of Pointers

To declare an array of pointers, specify the type of the pointer followed by the array name and its size.


int* ptrArr[5];  // Array of 5 pointers to int

This declaration creates an array named ptrArr of five integer pointers. Each element in ptrArr is a pointer that can hold the address of an integer variable.

Example 1: Array of Pointers to Integers

Here’s a basic example of using an array of pointers, each pointing to different integers.


#include <stdio.h>

int main() {
    int a = 10, b = 20, c = 30;
    int* ptrArr[3];  // Array of 3 pointers to int

    // Point each pointer to an integer variable
    ptrArr[0] = &a;
    ptrArr[1] = &b;
    ptrArr[2] = &c;

    // Accessing values through the array of pointers
    for (int i = 0; i < 3; i++) {
        printf("Value at ptrArr[%d]: %d\n", i, *ptrArr[i]);
    }

    return 0;
}


Explanation:

  • int* ptrArr[3]; creates an array of 3 integer pointers.

  • ptrArr[0] = &a;, ptrArr[1] = &b;, ptrArr[2] = &c; assigns each pointer to an integer variable's address.

  • *ptrArr[i] dereferences each pointer to access the values of a, b, and c.


Output:

Value at ptrArr[0]: 10
Value at ptrArr[1]: 20
Value at ptrArr[2]: 30

Example 2: Array of Pointers to Strings

A common use of an array of pointers is to store a list of strings, where each string is pointed to by an element in the array. This approach is memory-efficient, as each pointer can point to strings of varying lengths.



Explanation:

  • const char* fruits[] = {...}; declares an array of pointers to const char (strings).

  • Each element in fruits points to the beginning of a string (e.g., "Apple", "Banana").

  • fruits[i] gives the address of each string, and printing it as fruits[i] shows the entire string.



Output:

Fruit[0]: Apple
Fruit[1]: Banana
Fruit[2]: Cherry
Fruit[3]: Date

Example 3: Array of Pointers with Dynamic Memory Allocation

An array of pointers is useful for managing dynamically allocated arrays, where each pointer can point to a separate array of values. This approach is often used for creating a 2D array with rows of variable lengths.



Explanation:

  • int* ptrArr[3]; declares an array of 3 integer pointers.

  • Each element in ptrArr is dynamically allocated with varying sizes: 2, 3, and 4 integers.

  • Each row is initialized with some values and printed, and then the allocated memory is freed.


Output:

2D array with variable row lengths:
0 1 
1 2 3 
2 3 4 5 

Summary

  • Array of Pointers: Each element in the array is a pointer, enabling flexible data management.

  • Pointer to Integers: Useful for accessing and modifying multiple variables directly.

  • Pointer to Strings: Useful for storing lists of variable-length strings.

  • Dynamic Memory Allocation: Allows rows of different lengths in a 2D array by pointing each array element to separately allocated memory.


Using arrays of pointers provides flexibility, especially when dealing with variable-sized data, dynamic memory allocation, and complex data structures.


 
 
 

Comments


bottom of page