如何将将结构放在结构中?参考以下代码:
#include <stdio.h>
#include <string.h>
int main()
{
struct date
{
int month;
int day;
int year;
};
struct human
{
char name[45];
struct date birthday;
};
struct human president;
strcpy(president.name, "George Washington");
president.birthday.month = 2;
president.birthday.day = 22;
president.birthday.year = 1832;
printf("%s 出生于 %d/%d/%d\n",
president.name,
president.birthday.month,
president.birthday.day,
president.birthday.year);
system("pause");
return(0);
}
执行上面示例代码,得到以下结果:
George Washington 出生于 2/22/1832
上面的代码声明了两种结构类型:date
和human
。在human
结构体的声明中,会看到声明的date
结构变量:birthday
。然后它创建了一个human
结构体变量:president
。其余的代码用数据填充该结构的成员。它使用结构体的变量名称,而不是用于声明结构体的名称。