๐Ÿ“ C Pointers
Estimated reading: 4 minutes 7 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 :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

๐Ÿงฎ C Array of Pointers

Or Copy Link

CONTENTS
Scroll to Top