Arrays of Structures in C++
An array of structures stores a fixed-size sequence of records of the same structure type.
Summary
An array of structures stores a fixed-size sequence of records of the same structure type. Each element has its own independent members, keeping related fields together and reducing mismatches associated with separate arrays.
For struct Student { int id; double score; };, the declaration Student students[3]{}; creates three records whose numeric members are initialized to zero.
| Operation | Behavior |
|---|---|
| students[i].score | Selects an array element, then accesses its score member |
| students[1].score = 92.5; | Updates the second record's score |
| for (Student& student : students) | Traverses the original records by reference, allowing updates without copying |
Elements occupy contiguous storage, with successive elements separated by sizeof(Student) bytes, including any structure padding. A built-in array has a compile-time fixed extent. Valid indices satisfy , where is the number of elements. Built-in indexing does not check bounds; out-of-bounds access causes undefined behavior.
🧠 Key Concepts
- Record Types
- Member Access
- Fixed Array Extent
- Value Initialization
- Reference Traversal
- Contiguous Storage
- Structure Padding
- Array Bounds
- Undefined Behavior
🧠 Quick Check
See what you remember from the summary.
What best describes the expression students[i].score?
🧠 Flashcards Preview
Tap a card to reveal the definition.
Ready to quiz yourself?
Test what you remember with a full practice quiz on this note. Create a free account and start in seconds.
Full Notes
Read the original note content before deciding whether to save or study from it.
Arrays of Structures in C++
📘 Overview An array of structures stores multiple records of the same structure type in a fixed-size sequence. Each record groups related fields, and accessing a field requires selecting an array element before selecting its member.
🧠 Key Idea In students[i].score, students[i] selects one structure object and .score selects a member of that object. Each array element contains its own independent set of members.
⚔️ Core Details: - A structure defines a record type: struct Student { int id; double score; }; - Student students[3]{}; creates three Student objects with all id and score members initialized to zero. - The expression students[1].score = 92.5; assigns the score member of the second record. - The loop for (Student& student : students) { student.score = 0.0; } modifies each record through a reference. - Array elements occupy contiguous storage at intervals of sizeof(Student) bytes, including any padding within the structure. - Built-in array indexing performs no bounds checking; accessing an element outside the array causes undefined behavior.
🎯 Why It Matters: - Keeping related fields in one record prevents mismatches that can occur when separate arrays store corresponding attributes. - Traversing the array supports record-processing tasks such as searching by identifier, calculating score summaries, and sorting complete records. - Reference-based traversal avoids copying each structure and allows updates to affect the original records.
🧠 Quick Recall: - Member access - array[index].member selects a field within a particular record. - Valid index -
More ways to study when you copy this note
Copy this note into your library to unlock focused practice sessions and long-term review.
Answer all questions first, then see feedback at the end — the way real exams work.
Focuses each session on what you got wrong, not what you already know.
Full timed exam with all questions, no pausing, and results at the end. Built for board exam prep.
Part of 🗄️ BS Information Systems — 1st Year
Browse this Review Set →More Computer Engineering notes
See all →More in Programming Fundamentals
See all →More from NoteLib
Browse NoteLib's public notes →Copy this note to your library and get the full Study Pack instantly — summary, key concepts, and practice quiz included.