C 在结构内部设置typedef
我想让我的代码更容易阅读,所以我想把一个大的结构集替换成更具可扩展性的,但它不能编译C 在结构内部设置typedef,c,typedef,structure,C,Typedef,Structure,我想让我的代码更容易阅读,所以我想把一个大的结构集替换成更具可扩展性的,但它不能编译 typedef float vec_t; typedef vec_t vec3_t[3]; typedef struct{ int x; vec3_t point; } structure1; //This Works just fine and is what i want to avoid structure1 structarray[] = {
typedef float vec_t;
typedef vec_t vec3_t[3];
typedef struct{
int x;
vec3_t point;
} structure1;
//This Works just fine and is what i want to avoid
structure1 structarray[] = {
1,
{1,1,1}
};
//This is what i want to do but dont work
//error: expected '=', ',', ';', 'asm' or '__attribute__' before '.' token
structarray[0].x = 1;
structarray[0].point = {0,0,0};
int main()
{
//This is acceptable and works
structarray[0].x = 1;
//but this dont work
//GCC error: expected expression before '{' token
structarray[0].point = {1,1,1};
}
为什么它不编译?是的,如果我记得的话,问题是,{1,1,0}风格的构造只能用作初始值设定项,而你有理由想把它赋给变量。是的,如果我记得的话,问题是,{1,1,0}风格的构造只能用作初始值设定项,你有理由想把它分配给一个变量。谢谢你的帮助:DMan,这是一种结构,尽管它相当清晰,但会让我的皮肤蠕动。请注意,这是一种称为指定初始值设定项的功能。它是有效的C99,但不是有效的C89,也就是C90或ANSI C,因此它在没有C99编译器的平台上不起作用。多亏了这一点,它才能完成任务:DMan,这是一种结构,尽管它相当清晰,但它会让我的皮肤爬行。请注意,这是一种称为指定初始值设定项的功能。它是有效的C99,但不是有效的C89 aka C90或ANSI C,因此它在没有C99编译器的平台上无法工作。Rafe,感谢您修复拼写错误,我将尝试不再犯同样的错误。Rafe,感谢您修复拼写错误,我将尝试不再犯同样的错误。
structure1 structarray[] = {
[0].x = 1,
[0].point = { 0, 0, 0 },
};
// you can also use "compound literals" ...
structure1 f(void) {
return (structure1) { 1, { 2, 3, 4 }};
}