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; }; 

我想定义整个菜单系统:

 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"} } } } } }; 

但是当我尝试编译时,我会extra brace group at end of initializer错误消息extra brace group at end of initializer获得extra brace group at end of initializer

我尝试了许多不同的方法,但没有一个成功。 那么在C中使用复杂的结构是否可能呢?

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

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

应该

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

因为struct数组会松开单个大括号的声明。

问题是你声明了一个结构的指针,但实际上你需要一个结构数组。 大多数时间struct name*struct name[]将是“可互换的”(读K&R看它们不是同一个东西),但是在静态初始化的情况下,它必须被声明为一个数组,所以编译器可以期望它具有固定大小,因此它可以确定用于结构的内存量。

我需要改进我的答案,但我的主要观点是我不会期待类似int a* = {3,3,4,5}; 编译。 首先,作业的两侧类型不同。 其次,编译器如何知道它是数组的初始化列表而不是结构? 第三,它怎么知道它应该期望4个元素而不是5个?