. Advertisement .
..3..
. Advertisement .
..4..
An array is one of, if not the most used types of data in the programming world, especially at higher levels. Struct is also a very popular method of data assignment. However, not many people know how to combine them together.
For this reason, we prepared this article on array of structs in C.
Explaining Array Of Structs In C
Before we can explain in detail this problem, you must first grasp the array definition. In short, it’s a sequential collection consisting of only the same type of data.
There is no clear difference between arrays of structs and arrays of primitive data types on principle. However, the difference shows when we examine the structs, as they have elements that comprise different data types.
Let’s create a small struct called Employee as an example.
struct Employee {
int iD;
char employeeName[10];
float promotionScore;
};
Then, we can declare a new array of this structure:
struct Employee employeeRecord[3]
The result is a new array with three elements, each of them belonging to the Employee struct type. You can access the individual elements by utilizing []
(the index notation). There is also the . (dot)
operator for accessing members.
When utilizing the index notation, remember that employeeRecord[0]
points you to the 0th array element. In other words, it leads you to the first element of the list if you are not familiar with computer listing.
Taking this into consideration, you can access the element’s members by calling the element and using the . operator.
employeeRecord[0].iD
will refer you to the 0th element’s iD member. Similarly, employeeRecord[0].employeeName
and employeeRecord[0]. promotionScore
leads you to the other two components, respectively.
You can put everything into a complete program as shown below:
#include<string.h>
#include<stdio.h>
struct Employee {
int iD;
char employeeName[10];
float promotionScore;
};
int main(void) {
int count;
struct Employee employeeRecord[3]
printf("Enter 3 employees’ records:");
for(count = 0; count < 3; counter++) {
printf("\nEnter ID:");
scanf("%d",&employeeRecord[counter].iD);
printf("\nEnter Name:");
scanf("%s",&employeeRecord[counter].employeeName);
printf("\nEnter Promotion Score:");
scanf("%f",&employeeRecord[counter].promotionScore);
}
printf("\nEmployee Information List:");
for(count = 0; counter < 5; count++) {
printf("\nID:%d\t Name:%s\t Promotion Score :%f\n", employeeRecord[counter].iD,employeeRecord[counter].employeeName, employeeRecord[counter].promotionScore);
}
return 0;
}
Output:
Enter three employees’ records:
Enter ID: 1
Enter Name: Mike
Enter Promotion Score: 80
Enter ID: 2
Enter Name: Alice
Enter Promotion Score: 85
Enter ID: 3
Enter Name: James
Enter Promotion Score: 70
Employee Information List:
ID: 1 Name: Mike Promotion Score: 80
ID: 2 Name: Alice Promotion Score: 85
ID: 3 Name: James Promotion Score: 70
Manipulating An Array Of Structs In C Using malloc()
Another popular method to manipulate an array of structs in C is utilizing the malloc()
function. It is usually called dynamic memory allocation, as malloc()
is the shortened form for memory allocation. malloc()’s main use is to allocate one memory block in a dynamic way with a specified size.
It will return a type void pointer, which you can cast into any other form of your liking. Do keep in mind that this function will initialize all memory blocks with their default garbage value. Forgetting to get rid of this value can lead to unwanted error messages.
Here is the syntax of malloc()
:
ptrVariable = (cast-type*) malloc(byte-size)
Once you master it, you can even use it to do fancy stuff like converting strings.
Conclusion
After reading through this article, we hope that you now have a firm grasp on Array Of Structs In C. Once you master this aspect, it can help you tremendously in many high-level programming tasks.
Struct and array are some of the most used data types in the modern programming world, after all.
Leave a comment