在追踪一个非常不透明的错误时,我一直试图在没有警告或错误的情况下进行编译。我的代码的这一部分工作正常,但 gcc 提示大括号 - 它说缺少大括号 和 额外的大括号。我通常初始化得比较草率,但在这里我尽可能迂腐地用大括号表示每个逻辑包含级别。我真正关心初始化的唯一结构是最后一个,Ccfg。我以为我会逐渐构建它,因为它包含嵌套的其他结构,但显然即使是它前面的结构也根据 gcc 被错误初始化。
代码如下:
#define max_nodes 24
struct Cseg
{
int begin;
int arc;
int end;
};
struct Node
{
struct Cseg exit[4];
};
struct Core
{
int num_circles;
int num_nodes;
struct Node node[max_nodes];
};
struct Ccfg
{
struct Core core;
int dummy1;
int dummy2;
};
int main(void)
{
struct Cseg A = {0,1,2};
struct Node B =
{
{0,1,2}, {1,3,0}, {2,-1,3}, {0,-2,1}
};
struct Node C =
{
{0,1,2}, {1,3,0}
};
struct Core D =
{4, 4,
{
{ {0,1,2}, {1,3,0}, {2,-1,3}, {0,-2,1} },
{ {1,3,0}, {2,1,0}, {3,-2,1}, {2,-1,0} },
{ {3,1,2}, {0,1,2}, {1,-3,0}, {2,-3,1} }
}
};
struct Ccfg E =
{
{2, 2,
{
{ {0,1,1}, {0,2,1} },
{ {1,2,0}, {1,1,0} }
}
}
};
return 0;
}
一些初始化不完整;这是故意的。我真正的 ccfg 结构有更多字段,但我在这篇文章中对其进行了简化。如果有人能让我知道我做错了什么,我将非常感激。谢谢!
编辑:在我的工作代码中,struct Ccfg E 的初始化程序省略了最里面的大括号,并且工作正常(但 gcc 仍然警告我)。我将它们添加到此测试中,因为它们在逻辑上似乎是合适的,但它们实际上会生成错误 - 我不明白。
最佳答案
您在某些地方缺少大括号。具体来说,如果您有一个结构数组,则整个数组需要用大括号括起来;您只是包装每个结构条目。我只是根据需要添加了大括号,现在效果很好。 http://ideone.com/fork/HqxB9R
#define max_nodes 24
struct Cseg
{
int begin;
int arc;
int end;
};
struct Node
{
struct Cseg ex[4];
};
struct Core
{
int num_circles;
int num_nodes;
struct Node node[max_nodes];
};
struct Ccfg
{
struct Core core;
int dummy1;
int dummy2;
};
int main(void)
{
struct Cseg A = {0,1,2};
struct Node B =
{
{ {0,1,2}, {1,3,0}, {2,-1,3}, {0,-2,1} }
};
struct Node C =
{
{ {0,1,2}, {1,3,0} }
};
struct Core D =
{4, 4,
{
{ { {0,1,2}, {1,3,0}, {2,-1,3}, {0,-2,1} } },
{ { {1,3,0}, {2,1,0}, {3,-2,1}, {2,-1,0} } },
{ { {3,1,2}, {0,1,2}, {1,-3,0}, {2,-3,1} } }
}
};
struct Ccfg E =
{
{2, 2,
{
{ { {0,1,1}, {0,2,1} } },
{ { {1,2,0}, {1,1,0} } }
}
}
};
return 0;
}
关于c - gcc 对初始化程序中大括号的提示(嵌套结构),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28637334/