Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C-Struct的初始值设定项值太多_C_Gcc_Struct_Rectangles_Initializer - Fatal编程技术网

C-Struct的初始值设定项值太多

C-Struct的初始值设定项值太多,c,gcc,struct,rectangles,initializer,C,Gcc,Struct,Rectangles,Initializer,我从另一个站点获得了代码: 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

我从另一个站点获得了代码:

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 one rectangle is on left side of other
  if (Rectangle1->topLeft.x > Rectangle2->botRight.x || Rectangle2->topLeft.x > Rectangle1->botRight.x)
    return 0;

  // If one rectangle is above other
  if (Rectangle1->topLeft.y < Rectangle2->botRight.y || Rectangle2->topLeft.y < Rectangle1->botRight.y)
    return 0;

  return 1;
}
可以用。 它可以很好地编译,但是重叠检查总是返回false(这意味着,即使它们确实重叠,它也会返回,好像它们从未重叠一样) 因此,切换到Visual Studio,我在第二个点的大括号上得到了错误

{{x, y}, {
         ^
说我有“太多的初始化值”
为什么这只是VisualStudio中出现的一个错误而不是GCC,它能解释为什么重叠代码对我不起作用吗?我已经找了好几天这个问题的答案了D:

OP试图用以下命令错误地初始化指针


使用C11,如果代码需要在等式或赋值中间生成<代码>矩形*/code >,可以使用复合文字。< /P> 怀疑这是否适用于VisualStudio

否则就用老办法

Rectangle tmpR = {{x, y}, {x+width, y+height}};
Rectangle *thisR = &tmpR;

您正在尝试初始化指针。指针显然根本没有任何结构字段。我想你可能是想把
thisR
oldR
声明为
Rectangle
类型,而不是指向该类型的指针。谢谢你的提示,但是,代码仍然不起作用。我将检查VisualStudio是否仍然向我提供该错误,尽管您的代码中可能还有其他未显示的错误。但无法进一步帮助您,因为您尚未显示完整的代码。只能对已显示的零件进行注释。如果您需要进一步的帮助,请显示一个。您可能正在调用一些其他函数,这些函数期望指针作为参数,但忘记将
thisR
oldR
更改为
&thisR
&oldR
。或者您的代码类似于
thisR->topLeft
,现在应该是
thisR.topLeft
Rectangle *thisR = {{x, y}, {x+width, y+height}},
          *oldR  = {{x2, y2}, {x2+width2, y2+height2}};
Rectangle *thisR = &( (Rectangle) {{x, y}, {x+width, y+height}});
Rectangle tmpR = {{x, y}, {x+width, y+height}};
Rectangle *thisR = &tmpR;