Introduction
C++ and C structures (struct) are useful for grouping related data into one unit, but their true value is unlocked when combined with arrays. By defining an “Employee” structure and then declaring an array of that structure type, you can smartly manage “all employee data” within a single variable. This is extremely powerful when handling multiple records (rows of data) that have the same structure, much like a database table.
In this article, I will explain the fundamental techniques of data management: declaring an array of structures, storing data in each element, and processing them using a loop.
Sample Code for Array of Structures
This code defines a Student structure and creates an array to store information for three students. Then, it uses a for loop to display the information of all students stored in the array in order.
Complete Code
#include <iostream>
#include <string>
// Definition of "Student" structure
struct Student {
int id; // Student ID
std::string name; // Name
int grade; // Grade
};
int main() {
// 1. Declare an array of structs (for 3 students)
Student classRoster[3];
// 2. Assign values to members of each array element (struct)
// Data for the 1st person (Index 0)
classRoster[0].id = 101;
classRoster[0].name = "Ichiro Suzuki";
classRoster[0].grade = 2;
// Data for the 2nd person (Index 1)
classRoster[1].id = 102;
classRoster[1].name = "Hanako Sato";
classRoster[1].grade = 2;
// Data for the 3rd person (Index 2)
classRoster[2].id = 103;
classRoster[2].name = "Kenta Takahashi";
classRoster[2].grade = 1;
// 3. Display all elements of the array in a loop
std::cout << "--- Student Roster ---" << std::endl;
for (int i = 0; i < 3; i++) {
std::cout << "Student ID: " << classRoster[i].id
<< ", Name: " << classRoster[i].name
<< ", Grade: " << classRoster[i].grade << std::endl;
}
return 0;
}
Execution Result
--- Student Roster ---
Student ID: 101, Name: Ichiro Suzuki, Grade: 2
Student ID: 102, Name: Hanako Sato, Grade: 2
Student ID: 103, Name: Kenta Takahashi, Grade: 1
Code Explanation
Student classRoster[3];
This is the part where the array of structures is declared.
- Student: The name of the custom structure (data type).
- classRoster: The variable name of the array.
- [3]: Specifies the number of elements. This creates an array capable of storing 3 distinct sets of
Studentdata (indices 0, 1, and 2).
classRoster[0].id = 101;
To access a member of a structure within an array, you first specify the array index using square brackets [], and then specify the structure member using the dot ..
VariableName[IndexNumber].MemberName
Using this syntax, you can accurately specify “which member of which element in the array” you want to access.
for (int i = 0; i < 3; i++) { … }
Arrays of structures work very well with for loops. By using the loop counter variable i as the array index (like classRoster[i]), you can efficiently perform the same operation (in this case, display) on all elements of the array in sequence.
Summary
In this article, I explained how to manage multiple record data together using arrays of structures.
- You can declare them just like normal arrays:
StructName variableName[size];. - Access members of each element like this:
variableName[index].memberName. - By combining with
forloops, you can efficiently handle large amounts of record data.
This “Array of Structures” is a fundamental form of data management in C++ and C. It is a very important technique applicable in various situations, such as storing data read from CSV files or mimicking database tables.
