Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Definition
2.1.
Structure
3.
Implementation in C
4.
Frequently Asked Questions
4.1.
What are structures?
4.2.
What is an array of structures?
4.3.
In C, what is the difference between the terms struct and union?
4.4.
What is dynamic data structure?
4.5.
Are there any other Data Structures and Algorithms content in Coding Ninjas Studio?
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

Array of Structures

Author Saksham Gupta
0 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

The first thing that comes to our mind when we hear about programming is Array, and the second thing we hear a lot is structures. But what happens if we combine both of them together? Yes, we will get an array of structures. 

Introductory Image

In this blog, we will see in-depth about an array of structures.

Also See, Sum of Digits in C and C Static Function.

Definition

As the name suggests, an array of structures is an array whose date type is structure. Now, before getting into it. Let's first revise our concepts about structures.

Structure

Structure in C language is a user-defined data type that enables us to store the collection of different data types. Let's say I have to create a structure for a student which will store different properties such as age, roll-no. Then we will have the following code.

struct student{
   int age;
   int roll_no;
};


Now, what if there are 3 students in class and we have to store their data. It would be very difficult to create 3 different structures for each student and then use them. Here our arrays come to the rescue. We can create an array of structures that will store the data of all these 3 students in one place; thus, it can be easily accessed. Let’s see how we can do this by the following code.

You can also read about the dynamic arrays in c, and  Tribonacci Series

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Implementation in C

#include <stdio.h>
 
struct student
{
   int age;
   int roll_no;
   int marks;
};
 
int main(void)
{
 
   // Declaring array of structure.
   struct student arr[3];
 
   // Details of the first student.
   arr[0].marks = 80;
   arr[0].age = 17;
   arr[0].roll_no = 21;
 
   // Details of the second student.
   arr[1].marks = 90;
   arr[1].age = 18;
   arr[1].roll_no = 22;
 
   // Details of the third student.
   arr[2].marks = 80;
   arr[2].age = 19;
   arr[2].roll_no = 23;
 
   // Printing Details of the second student.
   printf("%s \n", "Details of the second student are ");
   printf("%s %d \n", "Age", arr[1].age);
   printf("%s %d \n", "Marks", arr[1].marks);
   printf("%s %d \n", "Roll Number", arr[1].roll_no);
 
   return 0;
}


Output

Details of the second student are  
Age 18 
Marks 90 
Roll Number 22 

So, we can see how easily we can access the student’s data as well.

Frequently Asked Questions

What are structures?

Structure in C language is a user-defined data type that enables us to store the collection of different data types. Let's say I have to create a structure for a student which will store different properties such as age, and roll-no.
 

What is an array of structures?

In C, an array of structures is a collection of multiple structure variables, each of which contains information about a different entity. In C, an array of structures is used to store data about multiple entities of various data types. The collection of structures is another name for the array of structures.
 

In C, what is the difference between the terms struct and union?

A struct is a collection of complex data structures stored in a memory block, each with its own memory location to make them all accessible at the same time. In contrast, in the union, all of the member variables are stored in the same memory location, which means that changing the value of one member variable will change the value of all other members.
 

What is dynamic data structure?

A dynamic data structure (DDS) is a memory organization or collection of data that can expand or contract in size, allowing a programmer to precisely control how much memory is used. Unused memory is allocated or de-allocated from the heap as needed to change the size of dynamic data structures.
 

Are there any other Data Structures and Algorithms content in Coding Ninjas Studio?

Yes, Coding Ninjas Studio allows you to practice coding as well as answer frequently asked interview questions. The more we practice, the more likely we are to acquire a job at our dream company.

Conclusion

In this article, We have discussed the structure that is available in many programming languages, and how we can implement an array of structures. Lastly, we implemented the program in C programming language.

If you think this blog has helped you enhance your knowledge about the above question, and if you would like to learn more, check out our articles 

 

Check out the following problems - 


You can also consider our Online Coding Courses such as the DSA in PythonC++ DSA CourseDSA in Java Course to give your career an edge over others.

Please upvote our blog to help other ninjas grow.

Happy Learning!

Previous article
Typedef in c
Next article
Structure sorting in C
Live masterclass