07-25-2023, 01:10 PM
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;
}
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;
}