C 将void*强制转换为作为指针的结构?
我有一个链表,其中每个节点的形式如下:C 将void*强制转换为作为指针的结构?,c,pointers,struct,casting,C,Pointers,Struct,Casting,我有一个链表,其中每个节点的形式如下: struct queueItem { struct carcolor *color; int id; }; typedef struct queueItem *CustDetails; 我想运行以下函数: extern void mix(struct carcolor *v); 但是,该函数在以下范围内运行: void foo(void *v) //v should be the pointer to the dequeued q
struct queueItem
{
struct carcolor *color;
int id;
};
typedef struct queueItem *CustDetails;
我想运行以下函数:
extern void mix(struct carcolor *v);
但是,该函数在以下范围内运行:
void foo(void *v) //v should be the pointer to the dequeued queueItem
{
//do other stuff
mix(v->color);
}
这会产生以下错误:
request for member ‘color’ in something not a structure or union
当函数原型为void foo(void*v)
时,如何访问struct carcolor*color
我尝试强制转换
(struct queueItem)v
,但没有成功。您需要强制转换到指向该结构的指针
mix(((struct queueItem *)v)->color);
在这些情况下,我喜欢做的是获取一个本地指针并使用它
void foo(void *v) //v should be the pointer to the dequeued queueItem
{
struct queueItem *localpointer = v;
//do other stuff
mix(localpointer->color);
}
您需要强制转换到指向该结构的指针
mix(((struct queueItem *)v)->color);
在这些情况下,我喜欢做的是获取一个本地指针并使用它
void foo(void *v) //v should be the pointer to the dequeued queueItem
{
struct queueItem *localpointer = v;
//do other stuff
mix(localpointer->color);
}
强制转换为
((struct queueItem*)v)->color
。强制转换为((struct queueItem*)v)->color
。您的意思是struct queueItem
您在哪里编写的struct carcolor
v
是指向queueItem
的指针,它包含指向struct carcolor
的指针。。。是的,我的意思是与v
最初的类型相同。答案已编辑。我想你也忘了编辑第一行。一旦问题解决,我会将你的答案标记为正确。你是说你在哪里写的struct-carcolor
v
是指向queueItem
的指针,它包含指向struct carcolor
的指针。。。是的,我的意思是与v
最初的类型相同。答案已编辑。我想你也忘了编辑第一行。一旦问题解决了,我会把你的答案标记为正确的。