Introduction
Nested structures let you put one structure inside another. This helps organize related data clearly.
Nested structures let you put one structure inside another. This helps organize related data clearly.
struct Inner {
int value;
};
struct Outer {
struct Inner innerData;
int otherValue;
};struct Date {
int day;
int month;
int year;
};
struct Event {
char name[50];
struct Date eventDate;
};struct Point {
int x;
int y;
};
struct Rectangle {
struct Point topLeft;
struct Point bottomRight;
};This program creates an Event with a nested Date. It prints the event name and date clearly.
#include <stdio.h>
struct Date {
int day;
int month;
int year;
};
struct Event {
char name[50];
struct Date eventDate;
};
int main() {
struct Event meeting = {"Team Meeting", {15, 6, 2024}};
printf("Event: %s\n", meeting.name);
printf("Date: %d/%d/%d\n", meeting.eventDate.day, meeting.eventDate.month, meeting.eventDate.year);
return 0;
}Remember to use the dot operator to access nested structure members.
You can nest structures multiple levels deep if needed.
Nested structures help organize complex data by putting one structure inside another.
Use dot notation to access inner members.
This keeps your code clean and easy to understand.
typedef struct {
int day;
int month;
int year;
} Date;
typedef struct {
char name[20];
Date birthday;
} Person;
int main() {
Person p = {"Alice", {15, 8, 1990}};
printf("%d-%d-%d", p.birthday.day, p.birthday.month, p.birthday.year);
return 0;
}struct Address {
char city[20];
int zip;
};
struct Employee {
char name[30];
struct Address addr;
};
int main() {
struct Employee emp;
emp.addr->zip = 12345;
return 0;
}Person and returns the birth year?
typedef struct {
int day;
int month;
int year;
} Date;
typedef struct {
char name[50];
Date birthday;
} Person;
// Function prototype here
Which of the following is the correct function definition?Pass the test and this pattern moves unknown