C:数组结构中的结构数组 [英] C: Array of structs in struct of arrays

查看:57
本文介绍了C:数组结构中的结构数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下结构:

struct menu_item{
    int id;
    char *text;
};

struct menu_tab{
    char *label;
    unsigned char item_count;
    struct menu_item *items;
};

struct menu_page{
    char *label;
    unsigned char tab_count;
    struct menu_tab *tabs;
};

struct Tmenu{
    unsigned char page_count;
    struct menu_page *pages;
};

我想定义整个菜单系统:

And I would like to define the whole menu-system:

struct Tmenu menu_test = {
    2,
    {
        "F1",
        2,
        {
            {
                "File",
                8,
                {
                    {1, "text  1"},
                    {2, "text2"},
                    {3, "text3333333"},
                    {4, "text4"},
                    {5, "Hello"},
                    {6, "42"},
                    {7, "world"},
                    {8, "!!!!!!!!"}
                }

            },
            {
                "File2",
                3,
                {
                    {11, "file2 text  1"},
                    {12, "blah"},
                    {13, "..."}
                }

            }
        }
    },
    {
        "F2",
        1,
        {
            {
                "File3",
                5,
                {
                    {151, "The Answer To Life"},
                    {152, "The Universe"},
                    {153, "and everything"},
                    {154, "iiiiiiiiiiiiiiiis"},
                    {42, "Fourty-Two"}
                }

            }
        }
    }
};

但是当我尝试编译时,在初始化程序的结尾出现了额外的括号组错误消息.

But when I try to compile, I get extra brace group at end of initializer error messages.

我尝试了许多不同的方法来做,但是没有一个成功.那么,像这样完全可以在C语言中使用复杂的结构吗?

I tried many different ways to do it, but none of them succeeded. So is it possible at all to use complex structures in C, like this?

推荐答案

不,这种用法是不可能的,至少在"old"(C89)C中不可用.结构文字不能用于初始化指针,因为它不能解决结构在内存中的位置的问题.

No, this usage is not possible, at least not in "old" (C89) C. A struct literal cannot be used to initialize a pointer to the structure in question, since that doesn't solve the question of where in memory the struct is located.

这篇关于C:数组结构中的结构数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆