易百教程

编写程序将两个结构体嵌套在一个结构体中

创建一个用于存储名字和姓氏的结构体,创建另一种用于存储日期的结构体:Date。使用名称为Human的结构体来托管它们。

实现代码

#include <stdio.h>
#include <string.h>

int main()
{
    struct id
    {
        char first[20];
        char last[20];
    };
    struct date
    {
        int month;
        int day;
        int year;
    };
    struct human
    {
        struct id name;
        struct date birthday;
    };
    struct human president;

    strcpy(president.name.first, "George");
    strcpy(president.name.last, "Washington");
    president.birthday.month = 2;
    president.birthday.day = 22;
    president.birthday.year = 1732;

    printf("%s %s was born on %d/%d/%d\n",
        president.name.first,
        president.name.last,
        president.birthday.month,
        president.birthday.day,
        president.birthday.year);
    system("pause");
    return(0);
}

执行上面示例代码,得到以下结果:

George Washington was born on 2/22/1732