Nearby lessons
60 of 72C - Array of Structures
An array of structures stores multiple records of the same structure type. For example, we can store details of many students in one array.
1. Create an Array of Structures
First, define the structure. Then create an array using the structure type. In this example, students[0] is the first student and students[1] is the second student.
Example01
2. Display All Students Using a Loop
Use a for loop to visit each array element. Use students[i].memberName to access a member of the current student.
Example02
3. Remember the Syntax
students[0]selects the first structure variable.students[0].nameaccesses the first student's name.- Array indexing starts from
0, so an array of three records uses indexes0,1, and2.
Common Mistakes
- Do not start the loop at index
1if you want to include the first record. - Do not use an index equal to the array length; for three records, valid indexes are 0 to 2.
- Use both the index and dot operator, such as
students[i].marks.
📝 Key Takeaways
- An array of structures stores multiple records.
- Each array element is one complete structure variable.
- Use an index to select a record and the dot operator to access its members.
- A loop can display all records without repeating the same code.
🧠 Test Your Knowledge
2 QuestionsProgress: 0 / 2