C语言中的调用结构
我有以下代码:C语言中的调用结构,c,struct,C,Struct,我有以下代码: typedef struct { double x, y; } point_t ; typedef struct { point_t a, b, c; } triangle_t; int read_point(point_t * const point) { int status = scanf(" (&lf,&lf)", &point_t.x, &point_t.y); return(status); } 我
typedef struct {
double x, y;
} point_t ;
typedef struct {
point_t a, b, c;
} triangle_t;
int read_point(point_t * const point) {
int status = scanf(" (&lf,&lf)", &point_t.x, &point_t.y);
return(status);
}
我试图读取用户为三角形顶点(点a、点b和点c)输入的x和y坐标。然而,在scanf函数中,我在“点t”的两个实例下都出现了一个奇怪的错误
不允许使用类型名称
发生了什么事?将其更改为:
int status = scanf(" (%lf,%lf)", &(point->x), &(point->y));
记住使用变量名点
,而不是类型名点
。还需要注意的是,必须在指针类型上使用运算符->
(这相当于取消对它的引用,然后使用成员运算符[p->x==(*p).x尝试此代码)
typedef struct {
double x;
double y;
} point_t ;
typedef struct {
point_t a;
point_t b
point_t c;
} triangle_t;
int read_point(point_t * point) {
int status = scanf(" (&lf,&lf)", point->x, point->y);
return(status);
}
我认为在struct中,您应该用它的类型声明每个字段;多重声明为int x,y
不起作用。
其次,您正在传递一个指针,因此要访问,您应该使用参数的名称(“点”是这种情况),这样point->field
或(*point).field
而不是&point
我强烈建议将&lf
更改为%lf
;)@丹尼尔菲舍尔的观点很好:)我甚至没有看那个部分。这解决了我的问题!我想我看错了。谢谢你的帮助!