• C Primer Plus(6) 中文版 第14章 结构和其他数据形式 14.5 嵌套结构


    14.5 嵌套结构
    有时,在一个结构中包含另一个结构(即嵌套结构)很方便。
    程序清单14.3 friend.c程序
    // friend.c -- example of a nested structure
    #include
    #define LEN 20
    const char * msgs[5] =
    {
        "    Thank you for the wonderful evening, ",
        "You certainly prove that a ",
        "is a special kind of guy. We must get together",
        "over a delicious ",
        " and have a few laughs"
    };

    struct names {                     // first structure
        char first[LEN];
        char last[LEN];
    };

    struct guy {                       // second structure
        struct names handle;           // nested structure
        char favfood[LEN];
        char job[LEN];
        float income;
    };

    int main(void)
    {
        struct guy fellow = {   // initialize a variable
            { "Ewen", "Villard" },
            "grilled salmon",
            "personality coach",
            68112.00
        };
        
        printf("Dear %s, \n\n", fellow.handle.first);
        printf("%s%s.\n", msgs[0], fellow.handle.first);
        printf("%s%s\n", msgs[1], fellow.job);
        printf("%s\n", msgs[2]);
        printf("%s%s%s", msgs[3], fellow.favfood, msgs[4]);
        if (fellow.income > 150000.0)
            puts("!!");
        else if (fellow.income > 75000.0)
            puts("!");
        else
            puts(".");
        printf("\n%40s%s\n", " ", "See you soon,");
        printf("%40s%s\n", " ", "Shalala");
        
        return 0;

    /* 输出:

    */

    注意如何在结构中创建嵌套结构。和声明int类型一样,进行简单的声明:
    struct names handle;
    其次,诸如如何访问嵌套结构的成员,这需要使用两次点运算符:
    fellow.handle.first;
    .运算符的结合性是从左往右的。 

  • 相关阅读:
    【23种设计模式】装饰模式(九)
    Vue3学习:如何在Vue3项目中创建一个axios实例
    C++回顾录04-构造函数
    FastestDet---模型训练
    专利申请被驳回,如何专利复审?
    AStyle使用小结
    realtek高清晰音频管理器怎么关闭的方法
    c++面试题目汇总-1-57
    Visual Studio使用Git忽略不想上传到远程仓库的文件
    IT 技术电子书 收藏
  • 原文地址:https://blog.csdn.net/weixin_40186813/article/details/126502451