是的,这个标题和我一样令人困惑。该代码可能有助于展示我想要实现的目标;
在一个函数中,我传递了这个;
RESTOUCH_POINT_STRUCT stConvertPoint;
stConvertPoint.usX = astLCDPoints[0].usX;
stConvertPoint.usY = astLCDPoints[0].usY;
Driver_Control((DRIVER_STRUCT*)pstResTouch->pstDisplay, CMD_CONVERT_POINT_TO_ORIENTATION, &stConvertPoint);
接收驱动函数,需要接受stConvertPoint
的地址作为void指针,然后对其进行转换以获得指向其成员usX
的指针,以及 usY
。然后,该值被传递给内部函数,该函数会更改 usX
和 usY
的值。
DRIVER_RETURN ILI9328_Control(DRIVER_STRUCT *pstDriver_, USHORT usID_, void *pvData_)
{
...
case CMD_CONVERT_POINT_TO_ORIENTATION:
{
DRAW_POINT_STRUCT* pstPoint = (DRAW_POINT_STRUCT*)pvData_;
USHORT *pusX;
USHORT *pusY;
pusX = (USHORT*)pstPoint->usX;
pusY = (USHORT*)pstPoint->usY;
ILI9328_ConvertPointOrientation(pstDriver_, pusX, pusY);
}
break;
假设这一切都有效,则调用函数中的 stConvertPoint.usX
和 stConvertPoint.usY
的值现在应该不同。
这有可能吗?指针和引用总是让我非常困惑。
最佳答案
假设(根据命名约定)usX
和 usY
成员只是 unsigned Short
(它们不是指针),那么你不想要:
pusX = (USHORT*)pstPoint->usX;
pusY = (USHORT*)pstPoint->usY;
这将创建无效且无用的指针。
相反,你想要:
pusX = &pstPoint->usX;
pusY = &pstPoint->usY;
这是指向该结构的两个成员的指针。
关于c - 函数采用结构体的 void 指针,并将指向其成员的指针传递给另一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10343407/