An array of structures combines the advantages of arrays and structures. It allows programmers to store multiple structure variables of the same type in a single array, making data management more efficient and organized.
Table of Contents
What is an Array of Structures in C?
An array of structures is a collection of structure variables stored in an array. Each element of the array is a separate structure that can store multiple related data items.For example, instead of creating separate structure variables for multiple students, an array of structures can be used to store information about all students in a single array.
Syntax:
Example:struct structure_name
{
data_type member1;
data_type member2;
};
struct structure_name array_name[size];
Explanation:struct Student
{
int rollNo;
float marks;
};
struct Student students[5];
In this example, students is an array containing five structure variables of type Student. Each element of the array can store a roll number and marks independently.
Accessing Elements of an Array of Structures
Structure members inside an array are accessed using the array index and the dot operator.Syntax:
Example:array_name[index].member_name
// C program to access the elements of
// array of structures
#include stdio.h
struct Student
{
int rollNo;
float marks;
};
int main()
{
struct Student students[2];
students[0].rollNo = 101;
students[0].marks = 89.5;
students[1].rollNo = 102;
students[1].marks = 92.0;
printf("Student 1 Roll No: %d\n", students[0].rollNo);
printf("Student 1 Marks: %.2f\n", students[0].marks);
printf("Student 2 Roll No: %d\n", students[1].rollNo);
printf("Student 2 Marks: %.2f\n", students[1].marks);
return 0;
}
Output:
Explanation:Student 1 Roll No: 101
Student 1 Marks: 89.50
Student 2 Roll No: 102
Student 2 Marks: 92.00
The array contains two structure elements. The first element is accessed using index 0, while the second element is accessed using index 1. The dot operator is used to access individual members of each structure.
Initializing an Array of Structures
Values can be assigned to all structure elements during declaration.Example:
// C program initializing array of structures
#include stdio.h
struct Student
{
int rollNo;
float marks;
};
int main()
{
struct Student students[3] =
{
{101, 85.5},
{102, 90.0},
{103, 88.5}
};
for(int i = 0; i 3; i++)
{
printf("Roll No: %d, Marks: %.2f\n",
students[i].rollNo,
students[i].marks);
}
return 0;
}
Output:
Explanation:Roll No: 101, Marks: 85.50
Roll No: 102, Marks: 90.00
Roll No: 103, Marks: 88.50
Each structure element is initialized using curly braces. The values are assigned according to the order of members defined in the structure.
Taking User Input in an Array of Structures
Array elements can also be filled using user input.Example:
// C program to take user input in
// array of structures
#include stdio.h
struct Student
{
int rollNo;
float marks;
};
int main()
{
struct Student students[2];
for(int i = 0; i 2; i++)
{
printf("Enter Roll Number and Marks: ");
scanf("%d %f",
&students[i].rollNo,
&students[i].marks);
}
printf("\nStudent Details:\n");
for(int i = 0; i 2; i++)
{
printf("Roll No: %d, Marks: %.2f\n",
students[i].rollNo,
students[i].marks);
}
return 0;
}
Output:
Explanation:Enter Roll Number and Marks: 101 87.5
Enter Roll Number and Marks: 102 91.0
Student Details:
Roll No: 101, Marks: 87.50
Roll No: 102, Marks: 91.00
The first loop accepts input for each structure element, while the second loop displays the stored data. This method is commonly used for handling multiple records.
Difference Between Structure and Array of Structures
| Basis of Comparison | Structures | Arrays of Structures |
|---|---|---|
| Number of Records | A structure variable stores information for only one record. | An array of structures stores information for multiple records. |
| Declaration | Only one structure variable is declared. | Multiple structure variables are declared in the form of an array. |
| Data Storage | It can store one set of related data. | It can store multiple sets of related data of the same type. |
| Memory Usage | Memory is allocated for a single structure object. | Memory is allocated for all structure elements in the array. |
| Application | It is suitable when only one record needs to be managed. | It is suitable when many records need to be stored and processed. |
Advantages of Array of Structures
- Stores Multiple Records Efficiently: An array of structures allows multiple records to be stored in a single variable. This reduces the need for creating many separate structure variables.
- Simplifies Data Management: Managing large collections of related data becomes easier because all records are stored in an organized manner.
- Supports Loop Operations: Loops can be used to access, update, and display all records efficiently. This reduces code duplication and improves maintainability.
- Improves Program Readability: Using an array of structures makes the code more structured and easier to understand, especially when dealing with large datasets.
- Suitable for Real-World Applications: Many practical applications, such as student management systems and employee databases, rely on arrays of structures to handle multiple records.
Common Mistakes While Using Array of Structures
1. Accessing Invalid Array Index: Trying to access an index outside the array size can lead to undefined behavior.Example:
If the array contains only five elements, valid indexes are from 0 to 4.students[5].rollNo = 101;
2. Forgetting the Array Index: A member cannot be accessed directly without specifying the array element.
Incorrect Code:
students.rollNo = 101;
Correct Code:
students[0].rollNo = 101;
3. Using Incorrect Member Names: Using a member name that does not exist in the structure causes a compilation error.
Incorrect Code:
students[0].age = 20;
4. Not Initializing Structure Elements: Using structure members before assigning values can produce garbage values and unexpected results.
5. Using Wrong Data Types During Input: Providing incorrect format specifiers in scanf() can result in incorrect data being stored.
Example:
The correct format specifier for a float value is %f.scanf("%d", &students[i].marks);
Applications of Array of Structures
- Student Management Systems: Schools and colleges use arrays of structures to store information about multiple students, including roll numbers, names, and marks.
- Employee Record Systems: Organizations use arrays of structures to maintain employee details such as IDs, salaries, and department information.
- Library Management Systems: Libraries use arrays of structures to manage information about books, authors, publishers, and prices.
- Inventory Management Systems: Businesses use arrays of structures to store product details such as product IDs, names, quantities, and prices.
- Banking Applications: Banks use arrays of structures to manage customer account information, balances, and transaction records efficiently.
Conclusion
An array of structures in C is a powerful feature that allows multiple structure records to be stored and managed efficiently. It combines the capabilities of arrays and structures, making it ideal for handling collections of related data.Understanding how to declare, initialize, access, and manipulate arrays of structures is important for building real-world applications such as student management systems, employee databases, inventory systems, and banking software.
Frequently Asked Questions
1. What is an array of structures in C?2. How do you access members of an array of structures?An array of structures is a collection of structure variables stored in an array, where each element represents a separate record.
students[0].rollNoMembers are accessed using the array index followed by the dot operator.
3. Why is an array of structures used?
4. Can loops be used with an array of structures?It is used to store and manage multiple records of the same structure type efficiently.
5. What is the difference between a structure and an array of structures?Yes, loops are commonly used to input, process, and display data stored in an array of structures.
A structure stores a single record, whereas an array of structures stores multiple records of the same structure type.
0 Comments