How to Declare and Initialize an Array of Pointers to a Structure in C?
Last Updated :
28 Nov, 2022
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.
Creating structure pointer arrays(Static Arrays)
i). 1D Arrays
We can statically allocate memory for 1D and 2D arrays in C language. The static memory is allocated in the stack memory. We can do static memory allocation of structure pointers in the following ways:
Step 1 - Declaring and initializing 1D arrays
Let's say we have a structure "node", and we created different pointers to this structure. Now, to make a 1D array of those pointers in static memory, we must follow the following syntax:
Syntax:
<struct_name> * <array_name> [ <size_of_array> ] ; // Declaration
<array_name> [ <index_number> ] = <pointer_to_the_structure> ; // Initialization
We can access the pointers inside our array just like we access normal array elements. There is just a little bit of modification that we need to use the arrow operator to access the data present inside our structure pointer.
Step 2 - Accessing the pointers within the array
Syntax:
<array_name> [<index_number>] -> <data to be accessed which is present inside the structure >
Example:
C
// C Program to implement
// structure pointer arrays
#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));
// Declaration of
// structure-pointer-array of size 2
node* struct_array[2];
// Initializing structure pointers
structure_ptr1->number = 100;
structure_ptr1->character = 'a';
structure_ptr2->number = 200;
structure_ptr2->character = 'b';
// Initializing this array with pointers
struct_array[0] = structure_ptr1;
struct_array[1] = structure_ptr2;
// Printing data of structures
printf("Data:\n");
printf("%d and %c\n", struct_array[0]->number,
struct_array[0]->character);
printf("%d and %c\n", struct_array[1]->number,
struct_array[1]->character);
return 0;
}
OutputData:
100 and a
200 and b
In the above example, we have a structure called a "node". We made 2 pointers to that structure namely- 'structure_ptr1' and 'structure_ptr2' and initialized them. After this, we declared an array - "struct_array" of size 2 and initialized it with our struct pointers.
ii). 2D Arrays
Step 1 - Declaring and initializing 2D arrays
Syntax:
<structure_name> * <array_name> [number_of_rows][number_of_columns];
<array_name> [row_number][column_number] = <pointer_to_structure> ;
Step 2- Accessing elements of our 2D array
Syntax:
< array_name >[ <row_number> ][ <column_number> ] -> <data to be accessed which is present inside the structure >;
Example:
C
// C Program to implement
// structure pointer 2-D array
#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));
// Declaration of
// structure-pointer-array
// of size 2 X 2
node* structure_array[2][2];
// 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';
// Initializing this array with pointers
structure_array[0][0] = structure_ptr1;
structure_array[0][1] = structure_ptr2;
structure_array[1][0] = structure_ptr3;
structure_array[1][1] = structure_ptr4;
// Accessing the values of these structure pointers from
// 2D 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");
}
}
OutputData:
a - 100 b - 200
c - 300 d - 400
So this is how we declare and initialize a 2D array of structure pointers. Here, we use the same structure - "node" and made 4 pointers for it which were - "structure_ptr1", "structure_ptr2", "structure_ptr3" and "structure_ptr4". After that, we declared a 2D array of size - 2 X 2 namely - structure_array.
Note: The data type of the array must be the same as that of the structure followed by * (asterisk) sign, which signifies array of structure pointers.
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");
}
}
OutputData:
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");
}
}
Outputa - 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.
Similar Reads
How to declare a Two Dimensional Array of pointers in C?
A Two Dimensional array of pointers is an array that has variables of pointer type. This means that the variables stored in the 2D array are such that each variable points to a particular address of some other element. How to create a 2D array of pointers: A 2D array of pointers can be created follo
3 min read
C# Program to Demonstrate the Array of Structures
An array of structures means each array index contains structure as a value. In this article, we will create the array of structure and access structure members using an array with a specific index. Syntax array[index].Structure(Details); where index specifies the particular position to be inserted
2 min read
How to instantiate Struct Pointer Address Operator in Golang?
As pointers are the special variables that are used to store the memory address of another variable whereas, the struct is user-defined data type that consists of different types. A struct is mainly a holder for all other data types. By using a pointer to a struct we can easily manipulate/access the
2 min read
Difference between pointer to an array and array of pointers
Pointer to an array: Pointer to an array is also known as array pointer. We are using the pointer to access the components of the array. int a[3] = {3, 4, 5 }; int *ptr = a; We have a pointer ptr that focuses to the 0th component of the array. We can likewise declare a pointer that can point to whol
4 min read
How to declare a 2D array dynamically in C++ using new operator
Prerequisite: Array BasicsIn C/C++, multidimensional arrays in simple words as an array of arrays. Data in multidimensional arrays are stored in tabular form (in row major order). Below is the general form of declaring N-dimensional arrays: Syntax of a Multidimensional Array: data_type array_name[si
4 min read
How to create an Array of Objects in the Stack memory?
What is an Array of Objects? An array of objects is a data structure that stores a collection of objects of the same type. The objects in the array are stored in contiguous memory locations, and the array provides indexed access to the objects. This means that you can access an individual object in
6 min read
How to Pass or Return a Structure To or From a Function in C?
A structure is a user-defined data type in C. A structure collects several variables in one place. In a structure, each variable is called a member. The types of data contained in a structure can differ from those in an array (e.g., integer, float, character). Syntax: struct geeksforgeeks { char nam
3 min read
C# Program to Implement an Interface in a Structure
Structure is a value type and a collection of variables of different data types under a single unit. It is almost similar to a class because both are user-defined data types and both hold a bunch of different data types. We can create a structure by using struct keyword. A structure can also hold co
2 min read
How to create a Struct Instance Using a Struct Literal in Golang?
A structure or struct in Golang is a user-defined data type which is a composition of various data fields. Each data field has its own data type, which can be a built-in or another user-defined type. Struct represents any real-world entity which has some set of properties/fields. For Example, a stud
4 min read
How to pass or return a structure to/from a Function in C/C++?
A structure is a user-defined data type in C/C++. A structure creates a data type that can be used to group items of possibly different types into a single type. How to pass structure as an argument to the functions? Passing of structure to the function can be done in two ways: By passing all the el
3 min read