我有一个结构和一个函数,它返回一个指向它读取的结构的指针:
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_t
,该函数返回指向cal_t
的指针。所以你需要取消引用指针。
cal = *lld_tpReadCalibration();
关于c - 从指针分配结构的成员值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13022317/