void * 类型指针变量如何赋值
struct _MyDataType
{
/* User data header */
UserDataType Type;
OpcUa_UInt16 Number; // 当前变量在该类型变量的序号
/* Protocol information */
void *pValue;
};
typedef struct _MyDataType MyDataType;
pRes->Results[i].Value.Value.Double = (OpcUa_Double)(*(pSensor->pValue)); //*(pSensor->pValue)的意思
编译时提示如下:
In function ‘xxxxx’: warning: dereferencing ‘void *’ pointer 108 | pRes->Results[i].Value.Value.Double = (OpcUa_Double)(*(pSensor->pValue)); //*(pSensor->pValue)的意思 | ^~~~~~~~~~~~~~~~~~ error: invalid use of void expression 108 | pRes->Results[i].Value.Value.Double = (OpcUa_Double)(*(pSensor->pValue)); //*(pSensor->pValue)的意思 | ^ make[2]: *** [output/CMakeFiles/output.dir/build.make:180:output/CMakeFiles/output.dir/u.c.o] 错误 1 make[1]: *** [CMakeFiles/Makefile2:385:output/CMakeFiles/output.dir/all] 错误 2
void*类型定义的指针变量只接收对象的地址,没有对象的类型概念。所以该指针变量是不能直接用;“*指针变量”去访问对象的,只能经强制类型转换后才能“间接”访问:*(type*)指针变量,必须给出正确的type!