Structure Declaration & Member Access
A structure is a blueprint for a record. It groups fields of different types into one object.
In plain English
A struct is a form with labeled boxes. Each box holds one fact about the same thing.
Declaration, initialization, and member access
A struct declaration defines a new type. Each member has its own type and name. An array is a homogeneous collection: every element has the same type. A structure is a heterogeneous collection: its members can have different types. Typical applications include records, linked nodes, configuration bundles, and function return values. Use the dot operator to access a member of a structure object. Initialization follows member order unless you use designated initializers, which are clearer and safer when fields change. Do not forget the semicolon after the closing brace.
struct Student {
int id;
char name[32];
double gpa;
};
struct Student s = {1, "Ada", 3.9};
printf("%s has GPA %.2f\n", s.name, s.gpa);
struct Student t = {.id = 2, .name = "Grace", .gpa = 3.8};Arrays as members
A structure member can be an array. The array is stored inside the structure, so copying the structure copies the array as well. Use strcpy or a bounded copy to fill a char array member; assignment does not work for arrays. The structure size includes the array plus any padding.
struct Classroom {
char label[16];
int seats;
};
struct Classroom room = {"Lab-A", 30};
printf("%s has %d seats\n", room.label, room.seats);Key takeaways
- struct groups heterogeneous fields into one type.
- Use . to access a member of a structure object.
- Designated initializers make field order explicit.
- Arrays inside structures are copied with the structure.
Common pitfalls
- Forgetting the semicolon after a struct definition.
- Trying to assign an array member directly.
- Assuming struct layout has no padding.
Key focus
- Trace member access and initialization.
- Know the difference between a structure type and a structure variable.
Quick check
1. Which operator accesses a member of a structure object?
2. What is missing if this declaration fails: struct Point { int x; int y; }