2011-01-20 61 views
1

我想让我的代码更易于阅读,所以我想将一个大的结构集合替换为更强的表达式,但它不能编译。在一个结构中设置typedef

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

为什么不编译?

+0

Rafe,谢谢你解决咒语错误,生病试着不要再犯同样的错误。 – banana 2011-01-20 04:09:23

回答

3
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 }}; 
} 
+0

谢谢,这将完成这项工作:D – banana 2011-01-20 04:06:16

+0

男人,这是那些虽然它是相当清晰的事实,使我的皮肤爬行的建设之一。 – 2011-01-20 04:09:46

1

呀,这个问题,如果我记得的是,{1,1,0}风格构造只能用作初始化,而你(合理的),希望将其分配给一个变量。