C Nested Structures
In this page:
struct Inner {
data_type member;
};
struct Outer {
struct Inner inner_name;
};
outer_variable.inner_name.member;
What is a Nested Structure?
For example, an Employee structure might contain an Address structure as one of its fields, letting you model has-a relationships between related pieces of data cleanly, similar to composition in object-oriented languages.
Example: What is a Nested Structure?
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Define a structure type named Address
struct Address {
char city[20];
};
// Define a structure type named Employee
struct Employee {
char name[20];
struct Address address;
};
// Program execution starts in main()
int main() {
struct Employee e = {"Alice", {"Springfield"}};
// Print formatted text to the screen
printf("%s %s", e.name, e.address.city);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Defining Nested Structures
The inner structure type must be fully defined (or at least declared) before the outer structure references it, since the compiler needs to know the inner structure's size to correctly lay out the outer structure in memory.
Example: Defining Nested Structures
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Define a structure type named Address
struct Address {
char street[30];
};
// Define a structure type named Employee
struct Employee {
char name[20];
struct Address address;
};
// Program execution starts in main()
int main() {
struct Employee e = {"Bob", {"123 Main St"}};
// Print formatted text to the screen
printf("%s", e.address.street);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Initializing Nested Structures
A nested initializer looks like Employee e = {"Alice", {"123 Main St", "Springfield"}} -- the inner braces group the Address fields separately from the outer Employee fields, mirroring the nesting in the type definition.
Example: Initializing Nested Structures
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Define a structure type named Address
struct Address {
char street[30];
char city[20];
};
// Define a structure type named Employee
struct Employee {
char name[20];
struct Address address;
};
// Program execution starts in main()
int main() {
struct Employee e = {"Alice", {"123 Main St", "Springfield"}};
// Print formatted text to the screen
printf("%s", e.address.city);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Accessing Nested Members
employee.address.city reads the city field by first navigating into the address member, then into its city field -- each dot operator moves one level deeper into the nested structure hierarchy.
Example: Accessing Nested Members
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Define a structure type named Address
struct Address {
char city[20];
};
// Define a structure type named Employee
struct Employee {
struct Address address;
};
// Program execution starts in main()
int main() {
struct Employee employee = {{"Springfield"}};
// Print formatted text to the screen
printf("%s", employee.address.city);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Nesting Arrays of Structures
This lets you model more complex real-world data, like a company with an array of departments, each containing an array of employee structures -- nesting arrays and structures together is how C represents hierarchical data without built-in classes.
Example: Nesting Arrays of Structures
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Define a structure type named Employee
struct Employee {
char name[20];
};
// Define a structure type named Department
struct Department {
struct Employee employees[2];
};
// Program execution starts in main()
int main() {
struct Department d = {{{"Alice"}, {"Bob"}}};
// Print formatted text to the screen
printf("%s %s", d.employees[0].name, d.employees[1].name);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- Accessing an inner member without the full path, such as
emp.cityinstead ofemp.address.city. - Leaving the inner struct uninitialized so its members contain garbage.
- Defining a struct that contains itself by value, which is not allowed; use a pointer instead.
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: