我已经声明了 2 个结构:point
(其中包含 2 个 double )和 circle
(其中包含一个点和圆的半径,一个 double )。
typedef struct {
double x;
double y;
} point;
typedef struct {
point m;
double r;
} circle;
为了创建一个圆圈,我使用了这两个函数:
point* read_point() {
point* p = malloc (sizeof(point));
printf("\nx middle:");
scanf("%lf",&p->x);
printf("\ny middle:");
scanf("%lf",&p->y);
return p;
}
circle* read_circle() {
circle* c = malloc(sizeof(circle));
point* p = read_point();
printf("\nradius circle:");
scanf("%lf",&c->r);
c->m = *p;
}
在我的 main
我声明了一个圆指针数组,添加一个圆,然后尝试打印出圆心和半径。 (此时我的程序崩溃了。)
circle* array[3];
array[0] = read_circle();
print_circle(array[0]); //Crashes
这是打印函数的样子:
void print_circle(circle* c){
point p = c->m;
printf("\n%lf",p.x);
printf("\n%lf",p.y);
printf("\n%lf",c->r);
}
最佳答案
read_circle
没有返回 c
(或任何东西)。
关于c - 取消引用结构指针数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31969802/