Skip to content
CurriculumChapter 24 ยท Aggregate Types
IntermediateHigh focus~16 min

Arrays of Structures & Nested Structures

Structures become powerful when you combine them: an array of records, a record inside a record, or a record with an anonymous inner structure.

In plain English

Nesting structs is putting a small form inside a bigger form. An array of structs is a stack of forms.

01

Arrays of structures and nested structures

An array of structures stores several records contiguously. Access a field through two indexes: students[i].name. A nested structure places one structure inside another, and you chain the dot operator: outer.inner.field. Nested structures model real records such as an address inside a customer.

chapter-24.cC
struct Address {
    char city[32];
    char pin[8];
};

struct Customer {
    char name[32];
    struct Address address;
};

struct Customer c = {"Ada", {"London", "N1"}};
printf("%s lives in %s\n", c.name, c.address.city);
02

Arrays of structures and anonymous structures

An array of structures is traversed with a loop. An anonymous structure has no tag, so it can only be used through its variables. Anonymous members can also appear inside another structure, making their fields accessible directly. Use these features when they simplify the model, not to save a few characters.

chapter-24.cC
struct Student {
    char name[32];
    int score;
};

struct Student class_list[3] = {
    {"Ada", 95},
    {"Grace", 91},
    {"Linus", 88}
};

for (int i = 0; i < 3; i++) {
    printf("%s: %d\n", class_list[i].name, class_list[i].score);
}

Key takeaways

  • An array of structures stores multiple records.
  • Nested structures chain dot access.
  • An array can be a member of a structure.
  • Anonymous structures have no tag and are used through variables.

Common pitfalls

  • Forgetting the extra dot for a nested member.
  • Mixing up the structure index and the member name.
  • Using an anonymous structure where a reusable type is needed.

Key focus

  • Trace nested member access and array indexing.
  • Know the difference between an array of structures and a structure containing an array.

Quick check

0 / 2 answered

1. If students is an array of struct Student, how do you access the name of the second student?

2. What is a nested structure?