C Programs Tutorials | IT Developer
IT Developer

C Programming - C Array of Pointers



Share with a Friend

C Programming - C Array of Pointers

C Array of Pointers

An array of pointers is an array where each element is a pointer that can store the address of another variable. It allows you to create arrays of strings, arrays of dynamically allocated memory, or even arrays of structures where each pointer can point to a different memory location.

Understanding Array of Pointers

  • In an array of pointers, each element is a pointer that stores the address of an element in memory, and the type of the array is determined by the pointer's type.
  • For example, an array of int pointers (int *arr[5]) means each element of the array is a pointer to an int.

Syntax for Array of Pointers:

C

type *array_name[size];

  • type: Data type of the elements that the pointers in the array will point to.
  • array_name: Name of the array.
  • size: Number of elements in the array.

Example of Array of Pointers:

  1. Array of Pointers to Integers

C

#include <stdio.h>

int main() {

    int num1 = 10, num2 = 20, num3 = 30;

    // Array of pointers to integers

    int *arr[3];

    // Assigning addresses of variables to the array elements

    arr[0] = &num1;

    arr[1] = &num2;

    arr[2] = &num3;

    // Accessing values using array of pointers

    printf("Value of num1: %d\n", *arr[0]);  // 10

    printf("Value of num2: %d\n", *arr[1]);  // 20

    printf("Value of num3: %d\n", *arr[2]);  // 30

    return 0;

}

Output:

Value of num1: 10

Value of num2: 20

Value of num3: 30

  1. Array of Pointers to Strings (Array of String Literals)

A common use of an array of pointers is when dealing with arrays of strings (which are actually arrays of character pointers).

C

#include <stdio.h>

int main() {

    // Array of pointers to string literals

    char *arr[] = {"Hello", "World", "Array", "of", "Pointers"};

    // Accessing and printing each string

    for (int i = 0; i < 5; i++) {

        printf("%s\n", arr[i]);

    }

    return 0;

}

Output:

Hello

World

Array

of

Pointers

  1. Array of Pointers to Arrays (2D Array)

An array of pointers can also be used to represent a 2D array, where each element of the array points to another array.

C

#include <stdio.h>

int main() {

    // Array of 3 pointers to arrays of 2 integers

    int *arr[3];

    // Initializing arrays of integers

    int arr1[] = {1, 2};

    int arr2[] = {3, 4};

    int arr3[] = {5, 6};

    // Assigning the addresses of arrays to the array of pointers

    arr[0] = arr1;

    arr[1] = arr2;

    arr[2] = arr3;

    // Accessing elements through array of pointers

    printf("arr[0]: %d, %d\n", arr[0][0], arr[0][1]);  // 1, 2

    printf("arr[1]: %d, %d\n", arr[1][0], arr[1][1]);  // 3, 4

    printf("arr[2]: %d, %d\n", arr[2][0], arr[2][1]);  // 5, 6

    return 0;

}

Output:

arr[0]: 1, 2

arr[1]: 3, 4

arr[2]: 5, 6

Advantages of Array of Pointers:

  1. Memory Efficiency:
    • It allows you to dynamically allocate memory for individual elements, which is useful in scenarios where the size of the elements may vary.
  2. Flexibility:
    • Each element in the array can point to different memory locations, making it flexible for handling dynamic data structures like linked lists, trees, etc.
  3. Efficient for Strings:
    • It is often used for handling strings in C because each element of the array can point to a different string (which is a character array).

Limitations of Array of Pointers:

  1. Pointer Arithmetic:
    • You need to be careful with pointer arithmetic, especially when accessing array elements. Incorrect pointer manipulation can lead to undefined behavior.
  2. Complexity:
    • Understanding and maintaining pointer-based structures can become complex for large programs, especially when combined with dynamic memory allocation.

Conclusion:

An array of pointers is a powerful and versatile construct in C programming. It allows you to store memory addresses of various data types, providing flexibility in data management, particularly when working with dynamically allocated memory or strings. Proper understanding and careful manipulation of array of pointers can enhance the performance and efficiency of your programs.