gcc - C - Struct has too many initializer values -
i have code site:
typedef struct { byte x, y; } point; typedef struct { point topleft; /* top left point of rectangle */ point botright; /* bottom right point of rectangle */ } rectangle; byte rectanglesoverlap(rectangle *rectangle1, rectangle *rectangle2) { // if 1 rectangle on left side of other if (rectangle1->topleft.x > rectangle2->botright.x || rectangle2->topleft.x > rectangle1->botright.x) return 0; // if 1 rectangle above other if (rectangle1->topleft.y < rectangle2->botright.y || rectangle2->topleft.y < rectangle1->botright.y) return 0; return 1; }
i under impression that
rectangle *thisr = {{x, y}, {x+width, y+height}}, *oldr = {{x2, y2}, {x2+width2, y2+height2}};
would ok use. compiles fine, overlap check returns false (meaning, returns if never overlap if do) switching visual studio, got error on second point's brace
{{x, y}, { ^
saying have "too many initializer values" why error presented in visual studio , not gcc, , can explain why overlap code never works me? i've been looking answer question days d:
op trying incorrectly initialize pointer following @kaylum
rectangle *thisr = {{x, y}, {x+width, y+height}}, *oldr = {{x2, y2}, {x2+width2, y2+height2}};
with c11, if code needs make rectangle *
in middle of equation or assignment, use compound literal.
rectangle *thisr = &( (rectangle) {{x, y}, {x+width, y+height}});
doubt if works visual studio
else use old-fashion way.
rectangle tmpr = {{x, y}, {x+width, y+height}}; rectangle *thisr = &tmpr;
Comments
Post a Comment