Lesson 16: Structures — struct & typedef
An array stores many values of the same type — but what if a student needs both a name (a string) and a grade (a number)? That's what struct is for: it groups related fields into one new type, e.g. struct Student { char name[20]; int grade; };. You access a field with a dot: s.grade = 95;, and fill
A struct is like a student file at the school office: one folder with labeled compartments — a slot for the name, a slot for the grade. Instead of scattering notes all over the office, everything stays together under one name.
- struct (structure)
- A new type that groups several related fields — even of different types — under one name.
- field (member)
- A variable inside a struct, like name or grade. Each field has its own type and name.
- the dot operator s.grade
- The way to access a field of a struct variable: variable name, dot, field name.
- typedef
- Gives a type a new name. typedef struct {...} Student; lets you declare Student s; without the struct keyword.
- array of structs
- Student class[30]; — thirty students, each with its own name and grade: class[0].grade.