在结构中设置typedef

时间:2011-01-20 03:53:44

标签: 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[] = {
                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};
}

为什么不编译?

2 个答案:

答案 0 :(得分: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 }};
}

答案 1 :(得分:1)

是的,如果我记得这个问题是{1,1,0}样式构造只能用作初始化器,而你(合理地)想要将它分配给变量。