正在构建一个处理结构的简单程序。逻辑很简单,但出于某种原因,我无法弄清楚如何将结构传递给函数。我在main()中声明了结构。我从搜索网站得到的印象是,唯一的方法是创建一个头文件并声明这种方式。这是真的吗?
main() {
struct Rect {
double x;
double y;
char color;
double width;
double height;
};
struct Rect a, b, *rec;
这是我试图传递的地方:
int chk_overlap(struct Rect *r1, struct Rect *r2) {
if(((r1->x + r1->width) >= r2->x) && ((r1->y) >= (r1->y - r2->height))){
return 1;
} else {
return 0;
}
}
这只是我尝试的一次迭代,当我像这样传递它时,我得到一个解除引用不完整的指针错误。我也尝试将其声明为
typedef struct Rect {
double x;
double y;
char color;
double width;
double height;
} Rect;
Rect a, b, *rec;
将其作为
传递int chk_overlap(Rect *r1, Rect *r2) {
编辑:这是我实际使用的功能
int check = 0;
check = check + chk_overlap(&a, &b);
答案 0 :(得分:1)
您应该在main之前声明结构。
struct Rect {
double x;
double y;
char color;
double width;
double height;
}
/* Place here the function definitions */
int main (int argc, char *argv[]) {
struct Rect a, b, *rec;
...
}
/* place here the function code */
由于您在主(> 中声明,因此无法在外面看到它,因此功能无法识别。
除此之外,您调用该函数(function(&a, &b)
)的方式看起来是正确的。