banana
banana

Reputation: 13

Set typedef inside of an structure

I want to make my code more easy to read, so i want to replace a big structure set, to something more expresive, but it doesn't compile.

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

Why doesn't it compile?

Upvotes: 1

Views: 835

Answers (2)

DigitalRoss
DigitalRoss

Reputation: 146073

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

Upvotes: 3

Hack Saw
Hack Saw

Reputation: 2781

Yeah, the problem, if I recall is that the {1,1,0} style construct can only be used as an initializer, and you (reasonably) want to assign it to a variable.

Upvotes: 1

Related Questions