C Programming Tutorial

C Programming Tutorial

Array of Pointers in C

Hello Everyone, In this article, we will cover the concept of Array of Pointers in C Programming. We will explain what an array of pointers is, how it works, and provide simple examples to help you understand its practical use in c programming. Whether you’re new to C or have some experience, this guide will help you to understand concet of Array of Pointers in C effectively. Let’s get started.

An array of pointers in C programming is an array where each element is a pointer, typically pointing to an address in memory. This can be useful for various purposes, such as managing arrays of different sizes or accessing strings. In this explanation, we will focus on using an array of pointers to store and access strings.

Let’s break it down step by step:

  1. Declare an array of pointers: In C, we can declare an array of pointers using the following syntax: data_type *array_name[array_size]; For example, an array of 5 pointers to integers would be declared as: int *array_of_int_ptrs[5];
  2. Assign values to the array: After declaring the array, we can assign values to each element. If we want to store strings, we can assign string literals directly to the array elements: char *array_of_strings[] = {“apple”, “banana”, “cherry”, “orange”, “grape”}; Here, we have an array of 5 pointers, each pointing to the starting address of a string.
  3. Access values from the array: To access the values stored in the array, we can use the array subscript operator [] and dereference the pointer using the * operator or without it when dealing with strings: printf(“%s\n”, array_of_strings[0]); // Output: apple

Now let’s see a complete example:

#include <stdio.h>
int main() {
    // Declare and initialize an array of pointers to strings
    char *array_of_strings[] = {"apple", "banana", "cherry", "orange", "grape"};

    // Calculate the number of elements in the array
    int num_elements = sizeof(array_of_strings) / sizeof(array_of_strings[0]);

    // Loop through the array and print each string
    for (int i = 0; i < num_elements; i++) {
        printf("array_of_strings[%d] = %s\\n", i, array_of_strings[i]);
    }

    return 0;
}

Output:

array_of_strings[0] = apple
array_of_strings[1] = banana
array_of_strings[2] = cherry
array_of_strings[3] = orange
array_of_strings[4] = grape

In this example, we created an array of pointers to strings, initialized it with five string literals, calculated the number of elements in the array, and looped through the array to print each string. This is a simple yet powerful way to use an array of pointers in C programming.

Continuing with the concept of an array of pointers in C, let’s explore another use case. We can use an array of pointers to manage a collection of arrays of different sizes. This can be helpful in situations where you don’t want to waste memory by allocating the same amount of space for each array, especially if they are of varying lengths.

Let’s look at an example of using an array of pointers to manage a collection of integer arrays with different lengths:

1. Declare and initialize the arrays:

    int array1[] = {1, 2, 3};
    int array2[] = {4, 5, 6, 7};
    int array3[] = {8, 9};
    

    2. Create an array of pointers to the integer arrays:

      int *array_of_ptrs[] = {array1, array2, array3};

      3. Define the size of each array:

        int array_sizes[] = {sizeof(array1) / sizeof(array1[0]), sizeof(array2) / sizeof(array2[0]), sizeof(array3) / sizeof(array3[0])};
        

        4. Access values from the array of pointers:

          printf("%d\\n", array_of_ptrs[1][2]); // Output: 6
          

          Now let’s see a complete example:

          #include <stdio.h>
          int main() {
              // Declare and initialize integer arrays of different sizes
              int array1[] = {1, 2, 3};
              int array2[] = {4, 5, 6, 7};
              int array3[] = {8, 9};
          
              // Create an array of pointers to the integer arrays
              int *array_of_ptrs[] = {array1, array2, array3};
          
              // Define the size of each array
              int array_sizes[] = {sizeof(array1) / sizeof(array1[0]), sizeof(array2) / sizeof(array2[0]), sizeof(array3) / sizeof(array3[0])};
          
              // Loop through the array of pointers and print each integer array
              for (int i = 0; i < sizeof(array_of_ptrs) / sizeof(array_of_ptrs[0]); i++) {
                  printf("array_of_ptrs[%d] = {", i);
                  for (int j = 0; j < array_sizes[i]; j++) {
                      printf("%d", array_of_ptrs[i][j]);
                      if (j < array_sizes[i] - 1) {
                          printf(", ");
                      }
                  }
                  printf("}\\n");
              }
          
              return 0;
          }
          

          Output:

          array_of_ptrs[0] = {1, 2, 3}
          array_of_ptrs[1] = {4, 5, 6, 7}
          array_of_ptrs[2] = {8, 9}
          

          In this example, we created an array of pointers to manage a collection of integer arrays with different lengths. We initialized the arrays and the array of pointers, defined the size of each array, and looped through the array of pointers to print each integer array. This demonstrates another useful application of an array of pointers in C programming.

          Share This Article