2011-01-20 64 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}風格構造只能用作初始化,而你(合理的),希望將其分配給一個變量。