Why is this array of structural assignments not compiling?

here is the 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 I do pt[0] = {100, 200} , the compiler continues to complain about

error: expected expression before '{' token

I really don't get it. Isn't this an expression before the assignment operator {token (=)?

I do not understand why an appointment would be a problem. the value in the pt address refers to the Point array. I just set the 0th item to this new point, and I know that assigning a structure in a format like {100,200} is legal if the elements inside this array are just fields.

+5
source share
1 answer

To assign, enter a value for the Point type to make a composite literal :

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

Live code using gcc

It is equivalent

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

ps The component literal is not available in the old strict C89 compiler. It is available in GCC for C89 as an extension, and in C99, a composite literal is the main function.

+6
source

Source: https://habr.com/ru/post/1205833/


All Articles