← The C Language
Lesson 40 of 77 · 10 XP
Nested structs
Theory and problem statement
Machine translation, not yet reviewed by a native speaker.
A field of a struct can itself be a struct:
struct date {
int day, month, year;
};
struct person {
char name[32];
struct date birthday; // a nested struct
};
struct person p = {"Kai", {14, 2, 2001}};
printf("%d\n", p.birthday.year); // 2001 — dots in a chain
This is how real data is modelled: an order has a delivery address, and an address has a city and a street.
A nested struct can be copied or passed to a function as a whole: print_date(p.birthday);.
Task. You're given the structs date and person. Read a name and a date of birth (name day month year), then the current year. Print name: N — how old the person turns this year.
Sign up for free to solve this task right in your browser: code editor, running, test checks and hints.