tgu*_*926 0 c struct pointers casting
我有一个链表,每个节点都有以下形式:
struct queueItem
{
struct carcolor *color;
int id;
};
typedef struct queueItem *CustDetails;
Run Code Online (Sandbox Code Playgroud)
我想运行以下功能:
extern void mix(struct carcolor *v);
Run Code Online (Sandbox Code Playgroud)
但是,该函数在此内部运行:
void foo(void *v) //v should be the pointer to the dequeued queueItem
{
//do other stuff
mix(v->color);
}
Run Code Online (Sandbox Code Playgroud)
这给出了错误:
request for member ‘color’ in something not a structure or union
Run Code Online (Sandbox Code Playgroud)
如何struct carcolor *color在函数原型时访问void foo(void *v)?
我试过铸造,(struct queueItem) v但那没用.
您需要转换为指向结构的指针.
mix(((struct queueItem *)v)->color);
Run Code Online (Sandbox Code Playgroud)
在这些情况下我喜欢做的是获取一个本地指针并使用它
void foo(void *v) //v should be the pointer to the dequeued queueItem
{
struct queueItem *localpointer = v;
//do other stuff
mix(localpointer->color);
}
Run Code Online (Sandbox Code Playgroud)