C 从指针指定结构的成员值
我有一个结构和一个函数,返回指向它读取的结构的指针:C 从指针指定结构的成员值,c,pointers,struct,assign,C,Pointers,Struct,Assign,我有一个结构和一个函数,返回指向它读取的结构的指针: typedef struct cal_t { float xm; float ym; float xn; float yn; } cal_t; struct cal_t *lld_tpReadCalibration(void); 在其他地方,我确实有该结构的一个实例: struct cal_t cal; 现在我需要将该结构实例的值赋给我得到指针返回的结构的值。所以我想要的是cal.xm与lld_
typedef struct cal_t {
float xm;
float ym;
float xn;
float yn;
} cal_t;
struct cal_t *lld_tpReadCalibration(void);
在其他地方,我确实有该结构的一个实例:
struct cal_t cal;
现在我需要将该结构实例的值赋给我得到指针返回的结构的值。所以我想要的是cal.xm与lld_tpReadCalibration()中的cal->xm的值相同。象征性地:
struct cal_t cal;
cal = lld_tpReadCalibration();
但这当然行不通:
error: incompatible types when assigning to type 'volatile struct cal_t' from type 'struct cal_t *'
我怎样才能使它按我想要的方式工作
谢谢您的帮助。您需要以某种方式取消对指针的引用。您正在从函数返回指针,因此您正在查找
*
运算符或->
,这当然是*
的同义词
将cal
定义为struct-cal\u t
,函数返回指向cal\u t
的指针。因此,您需要取消对指针的引用
cal = *lld_tpReadCalibration();
您需要以某种方式取消对指针的引用。您正在从函数返回指针,因此您正在查找
*
运算符或->
,这当然是*
的同义词
将cal
定义为struct-cal\u t
,函数返回指向cal\u t
的指针。因此,您需要取消对指针的引用
cal = *lld_tpReadCalibration();
函数返回值是struct cal_t*,它是指针类型 因此,您应该将返回值指定给类型为struct cal_t*的变量 比如说,
struct cal_t *cal_ptr;
cal_ptr = lld_tpReadCalibration();
函数返回值是struct cal_t*,它是指针类型 因此,您应该将返回值指定给类型为struct cal_t*的变量 比如说,
struct cal_t *cal_ptr;
cal_ptr = lld_tpReadCalibration();