简体   繁体   English

C:数组结构中的结构数组

[英]C: Array of structs in struct of arrays

I have these structures: 我有以下结构:

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. 但是当我尝试编译时, extra brace group at end of initializer错误消息的extra brace group at end of initializer得到了extra brace group at end of initializer

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? 那么,像这样完全可以在C语言中使用复杂的结构吗?

不,这种用法是不可能的,至少在“旧”(C89)C中是不可能的。不能使用结构文字来初始化指向所讨论结构的指针 ,因为这不能解决结构在内存中的位置的问题位于。

struct Tmenu menu_test = {
    2,
    {
        "F1",
        2,
        {DATAFILE...},
        {DATAFILE2...}
    },

Should be 应该

struct Tmenu menu_test = {
    2,
    {
        "F1",
        2,
        {
        {DATAFILE...},
        {DATAFILE2...}
        }
    },

because the array of struct will loose the declaration for single braces. 因为struct数组将放宽单括号的声明。

the problem is that you declared a pointer to a struct, but in fact you want an array of structs. 问题是您声明了一个指向结构的指针,但实际上您想要一个结构数组。 Most of time struct name* and struct name[] will be "interchangeable" ( Read K&R to see that they are not the same thing), but in case of static initialization it must be declared as an array so the compiler can expected it to be of fixed size and therefore it can determine the amount of memory to be used for the struct. 大多数时候, struct name*struct name[]将是“可互换的”(请阅读K&R,以了解它们不是同一件事),但是在静态初始化的情况下,必须将其声明为数组,以便编译器可以期望将其声明为数组。是固定大小的,因此可以确定用于该结构的内存量。

I need to improve my answer,but my main point here is that I would not expect something like int a* = {3,3,4,5}; 我需要改善答案,但是我的主要观点是,我不会期望像int a* = {3,3,4,5}; to compile. 编译。 First the type is different on both sides of the assignment. 首先,分配双方的类型都不同。 Second how can the compiler know it is an initialization list for an array and not an struct? 其次,编译器如何知道它是数组的初始化列表而不是结构的初始化列表? Third, how can it know that it should expect 4 elements and not 5? 第三,如何知道应该期待4个元素而不是5个?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM