![]() |
When dealing with a large set of related data and different data types, organizing and managing it efficiently is crucial. In C programming, the combination of arrays and structures i.e. array of structures provides a powerful tool for managing that. In this article, we discuss the concept of an Array of Structures in C. What is Array?The array is a homogeneous collection of elements stored in the continuous memory location. The size of the array is fixed and we can randomly access the elements using their index. Declaration of Array array_type array_name[size]; What is Structure?The structure is one of the user-defined data types in C that can contain elements of different types as its members. Declaration of a Structure in C struct structure_name{ Array of Structures
Need for Array of StructuresSuppose we have 50 employees and we need to store the data of 50 employees. So for that, we need to define 50 variables of struct Employee type and store the data within that. However, declaring and handling the 50 variables is not an easy task. Let’s imagine a bigger scenario, like 1000 employees. So, if we declare the variable this way, it’s not possible to handle this. struct Employee emp1, emp2, emp3, .. . ... . .. ... emp1000; For that, we can define an array whose data type will be struct Employee soo that will be easily manageable. Declaration of Array of Structuresstruct structure_name array_name [number_of_elements]; Initialization of Array of StructuresWe can initialize the array of structures in the following ways: struct structure_name array_name [number_of_elements] = { The same initialization can also be done as: struct structure_name array_name [number_of_elements] = { GNU C compilers supports designated initialization for structures so we can also use this in the initialization of an array of structures. struct structure_name array_name [number_of_elements] = { Example of Array of Structure in CC
Output Emplyee ID: 0 - Employee Name: Amit |
Reffered: https://www.geeksforgeeks.org
Arrays |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 13 |