Monday 15 July 2013

c - Why doesn't this array of structure assignment compile? -



c - Why doesn't this array of structure assignment compile? -

here code struct point_tag { int x; int y; }; typedef struct point_tag point; int main(void) { point pt[] = { {10,20}, {30, 40}, {50,60}}; pt[0] = {100,200}; }

when pt[0] = {100, 200}, compiler keeps complaining of

error:expected look before '{' token

i don't though. isn't look before { token assignment operator(=)?

i don't understand why assignment issue though. value @ address pt refers array of point. setting 0th point new point , know assigning struct in format {100,200} legal elements within array fields.

for assignment, typecast value type point create compound literal:

pt[0] = (point){100,200};

live code using gcc

this equivalent to

{ point temp = {100,200}; pt[0] = temp; }

p.s. compound literal not available in old strict c89 compliant compiler. avilable in gcc c89 extension , in c99 compound literal core feature.

c arrays struct

No comments:

Post a Comment