๐Ÿ“ C Pointers
Estimated reading: 4 minutes 274 views

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 CaseDescription
String tableStoring an array of variable-length strings
Dynamic arraysPoint to different memory blocks
Function tablesArray of function pointers
Matrix (2D array) simulationArray 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 :
Share

๐Ÿงฎ C Array of Pointers

Or Copy Link

CONTENTS
Scroll to Top