QP School

Full Version: Nested structures in C
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Structures can be nested within each other.

struct Address {
    char city[50];
    char state[50];
};

struct Person {
    char name[50];
    int age;
    struct Address address;
};

int main() {
    struct Person p1;
    strcpy(p1.name, "John");
    p1.age = 30;
    strcpy(p1.address.city, "New York");
    strcpy(p1.address.state, "NY");
    printf("Name: %s, Age: %d, City: %s, State: %s\n", p1.name, p1.age, p1.address.city, p1.address.state);
    return 0;
}