Linux C Programming tutorial part 27 - Array of structures

In this ongoing C programming tutorial series, we have been discussing lately about structures. We have already covered the basics of structures, how structures can be used with functions, as well as the concept of pointers to structures.

Now, expanding further on the concept of structures, we'll discuss how to create an array of structures using easy to understand examples.

Array of structures in C programming language

So let's begin. Here's a simple structure named 'student_details' which as you can see, can be used to store roll number and marks for students:

struct student_details{
int roll_no;
int marks;
};

Now, suppose there are 5 students in total, and the requirement is to accept input from user about these student details. Then one way to write code for this is:

#include <stdio.h>

struct student_details{
int roll_no;
int marks;
};

int main()
{
struct student_details obj1;
struct student_details obj2;
struct student_details obj3;
struct student_details obj4;
struct student_details obj5;
...
...
...

Or may be something like this:

#include <stdio.h>

struct student_details{
int roll_no;
int marks;
};

int main()
{
struct student_details obj1, obj2, obj3, obj4, obj5;
...
...
...

Now, there's no harm if you follow the aforementioned approaches. The only concern that arises is whether these approaches are scalable. I mean, what if instead of 5, you need to create, say, 50 objects or instances. In that case, following these approaches will definitely make code writing and maintenance much more complex.

So is there a solution? Well, yes. A better way is to create an array of 'student_details' structure. Here's how:

struct student_details obj[5];

Following is a complete program that utilizes this structure array:

#include <stdio.h>

struct student_details{
int roll_no;
int marks;
};

int main()
{
struct student_details obj[5];
int i;

for(i=0;i<5;i++)
{
printf("\n Enter roll number: ");
scanf("%d", &(obj[i].roll_no));
printf("\n Enter marks (out of 100): ");
scanf("%d", &(obj[i].marks));
}

printf("\n Here's the data you've entered::");

for(i=0;i<5;i++)
{
printf("\n Roll number: %d", (obj[i].roll_no));
printf("-- Marks: %d", (obj[i].marks));
}


return 0;
}

So here, we used the 'student_details' structure we discussed earlier, and created an array of 5 objects of its type. Then, the program asks user to input student roll number and marks one by one. Info corresponding to each student is stored in each instance of structure array. And finally, the stored info is printed back as output to user.

Here's the output in my case:

Enter roll number: 1 

Enter marks (out of 100): 67

Enter roll number: 2

Enter marks (out of 100): 73

Enter roll number: 3

Enter marks (out of 100): 56

Enter roll number: 4

Enter marks (out of 100): 52

Enter roll number: 5

Enter marks (out of 100): 85

Here's the data you've entered::
Roll number: 1-- Marks: 67
Roll number: 2-- Marks: 73
Roll number: 3-- Marks: 56
Roll number: 4-- Marks: 52
Roll number: 5-- Marks: 85

Using this approach, you can easily scale your program. This means, even if there are 50 students whose information needs to be stored and printed, you just need to increase the size of array to 50, and make very few related changes. Rest of the code remains the same. So, in a nutshell, you can handle scalability easily when using arrays as compared to the approaches we discussed in the beginning of the tutorial.

Conclusion

In this tutorial, we covered the basic concept of array of structures. Using easy to understand examples, we discussed why a structure array is important, when to use it, and how to use it. You are advised to try out the examples from this tutorial on your machine. And in case of any doubt or query, leave a comment below.

Share this page:

0 Comment(s)