Creating structure pointer arrays (Dynamic Arrays)

i). 1D Arrays

As we know that in C language, we can also dynamically allocate memory for our variables or arrays. The dynamically allocated variables or arrays are stored in Heap. To dynamically allocate memory for structure pointer arrays, one must follow the following syntax:

Syntax:

< structure_name >  ** < array_name > = <structure_name **> malloc ( sizeof( <structure_name> )* size ) ;

Notice the double-pointer after the structure name and during typecasting malloc to the structure. This double pointer is necessary because we are pointing to an array of structure pointers, and we know that we use double pointers if we want to point to another pointer. 

Example:

C




// C Program to implement
// Dynamic Array of structure
// pointer
#include <stdio.h>
#include <stdlib.h>
 
typedef struct node {
    int number;
    char character;
} node;
 
int main(void)
{
    // First pointer to structure
    node* structure_ptr1 = (node*)malloc(sizeof(node));
    // Second pointer to structure
    node* structure_ptr2 = (node*)malloc(sizeof(node));
    // Third pointer to structure
    node* structure_ptr3 = (node*)malloc(sizeof(node));
    // Fourth pointer to structure
    node* structure_ptr4 = (node*)malloc(sizeof(node));
 
    // Initializing structure pointers
 
    structure_ptr1->number = 100;
    structure_ptr1->character = 'a';
 
    structure_ptr2->number = 200;
    structure_ptr2->character = 'b';
 
    structure_ptr3->number = 300;
    structure_ptr3->character = 'c';
 
    structure_ptr4->number = 400;
    structure_ptr4->character = 'd';
 
    // Declaring the array dynamically for structure
    // pointers
 
    // Note that double pointer is
    // used for pointer to pointer
    node** structure_array
        = (node**)malloc(sizeof(node) * 4);
 
    // Initializing the array of structure pointers
 
    structure_array[0] = structure_ptr1;
    structure_array[1] = structure_ptr2;
    structure_array[2] = structure_ptr3;
    structure_array[3] = structure_ptr4;
 
    // Accessing dynamic 1D arrays - just like static 1D
    // arrays
    printf("Data:\n");
    for (int i = 0; i < 4; i++) {
        printf("%c - ", structure_array[i]->character);
        printf("%d\t\t", structure_array[i]->number);
        printf("\n");
    }
}


Output

Data:
a - 100        
b - 200        
c - 300        
d - 400        

You can clearly see in the above code how we have initialized the structure pointer array at the end. The difference between this and static arrays is just that static arrays are allocated in stack memory, while these are allocated in heap memory. And so, you can resize these arrays anytime you want.

We can access these arrays just like we did with the static ones so there is no change in syntax for that.

Note: In case of static arrays, you can initialize the array all at once during the time of initialization like –  node *struct_arr [10 ] = { struct_ptr1, struct_ptr2, struct_ptr3 ….. }, whereas dynamically allocated arrays need to be initialized index-by-index.

ii).  2D Arrays

To allocate dynamic 2D arrays of pointers, first, we will create a 1D array dynamically and then will create sub-arrays at each index of that 1D array (basically an array of arrays containing pointers). Now, this requires the use of triple-pointers. We will understand this with the help of a diagram:

 

Explanation of the above figure:

  • *** st_arr is a triple pointer used to access the 2D array of structure pointers. Now, it is made a triple pointer because we are accessing those arrays, whose each element has a pointer to another array (subarray). And each element of those sub-arrays, have a pointer to the structure.
  • Each index of st_arr[i] contains sub-arrays.
  • Each index of sub-arrays – st_arr[i][j] contains a pointer to the structure.
  • st_ptr is a pointer to the structure.

Example:

C




// C Program to implement
// 2D arrays of structure
#include <stdio.h>
#include <stdlib.h>
 
typedef struct node {
    int number;
    char character;
} node;
 
int main(void)
{
    // First pointer to structure
    node* st_ptr1 = (node*)malloc(sizeof(node));
    // Second pointer to structure
    node* st_ptr2 = (node*)malloc(sizeof(node));
    // Third pointer to structure
    node* st_ptr3 = (node*)malloc(sizeof(node));
    // Fourth pointer to structure
    node* st_ptr4 = (node*)malloc(sizeof(node));
 
    // Initializing structure pointers
    st_ptr1->number = 100;
    st_ptr1->character = 'a';
 
    st_ptr2->number = 200;
    st_ptr2->character = 'b';
 
    st_ptr3->number = 300;
    st_ptr3->character = 'c';
 
    st_ptr4->number = 400;
    st_ptr4->character = 'd';
 
    // Declaring the array dynamically for structure
    // pointers    Step - 1
    // Note that double pointer is
    // used for pointer to pointer
    node*** structure_array
        = (node***)malloc(sizeof(node***) * 2);
 
    // Used double pointers, at each index to point to
    // arrays of structure pointers - Step 2
    for (int i = 0; i < 2; i++) {
        structure_array[i] = (node**)malloc(sizeof(node**));
    }
 
    // Initializing the array of structure pointers   - Step
    // 3
    structure_array[0][0] = st_ptr1;
    structure_array[0][1] = st_ptr2;
    structure_array[1][0] = st_ptr3;
    structure_array[1][1] = st_ptr4;
 
    // Printing the values of these structure pointers from
    // array
    printf("Data:\n");
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++) {
            printf("%c - ",
                   structure_array[i][j]->character);
            printf("%d\t\t", structure_array[i][j]->number);
        }
        printf("\n");
    }
}


Output

a - 100        b - 200        
c - 300        d - 400        
  • In the above code, as we have already discussed that triple pointer was used to point to an array of arrays containing structure pointers.
  • In the second step, we used double pointers as we had to use a pointer, which could point to an array of structure pointers.
  • In the third step, we initialized our dynamic 2D array, just like the static 2D array, index by -index.


How to Declare and Initialize an Array of Pointers to a Structure in C?

Prerequisite:

In C language, arrays are made to store similar types of data in contiguous memory locations. We can make arrays of either primitive data types, like int, char, or float, or user-defined data types like Structures and Unions. We can also make arrays of pointers in C language. Today we will learn how to create arrays of Structure Pointers or pointers-to-structure in C language, both Statically and Dynamically.

Similar Reads

Creating structure pointer arrays(Static Arrays)

i). 1D Arrays...

Creating structure pointer arrays (Dynamic Arrays)

...