将void *转换为一个指针结构?

时间:2014-04-09 12:09:57

标签: 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 queueItem
{
    //do other stuff
    mix(v->color);
}

这给出了错误:

request for member ‘color’ in something not a structure or union

当函数原型为struct carcolor *color时,如何访问void foo(void *v)

我尝试投射(struct queueItem) v,但这不起作用。

1 个答案:

答案 0 :(得分:5)

你需要转换为指向结构的指针。

    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);
}
相关问题