**Nested Structures** allow you to organize data into layers. Instead of having a flat list of variables, you can group related attributes into their own small structures and then embed those smaller structures into larger ones. This promotes code reusability and logical organization.
There are two ways to nest structures: by defining the inner structure outside the main structure, or by defining it inside.
This is the professional standard because it allows you to reuse the inner structure (like Address) in multiple other structures (like Employee or Customer).
To access the members of a nested structure, you use the Dot Operator (.) twice. You first access the inner structure variable, and then the specific member within it.
When initializing a nested structure in a single line, use nested curly braces { { } } to keep the data organized according to the structure's hierarchy.
You can also define the inner structure directly inside the outer one. However, this structure cannot be used elsewhere in your program.
If you are using a pointer to the outer structure, you use the Arrow Operator (->) for the first level, but if the inner level is not a pointer, you stick to the dot operator for the nested member.
| Feature | Flat Structure | Nested Structure |
|---|---|---|
| Organization | Scattered variables. | Logical grouping. |
| Readability | Moderate. | High (Self-documenting). |
| Access Level | Single dot (s.id). |
Double dot (s.dob.day). |
typedef makes your code look incredibly clean and professional.