简体   繁体   English

将void *转换为指针的结构?

[英]Casting void * to a struct which is a pointer?

I have a linked list, where each node is of the following form: 我有一个链接列表,其中每个节点的格式如下:

struct queueItem
{
    struct carcolor *color;
    int id;
};
typedef struct queueItem *CustDetails;

I want to run the following function: 我想运行以下功能:

extern void mix(struct carcolor *v);

However, the function is run inside this: 但是,该函数在其中运行:

void foo(void *v)    //v should be the pointer to the dequeued queueItem
{
    //do other stuff
    mix(v->color);
}

This gives the error: 这给出了错误:

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

How can I access struct carcolor *color when the function prototype is void foo(void *v) ? 当函数原型为void foo(void *v)时,如何访问struct carcolor *color

I tried casting (struct queueItem) v but that didn't work. 我试过强制转换(struct queueItem) v但是没有用。

You need to cast to a pointer to the structure. 您需要转换为结构的指针。

    mix(((struct queueItem *)v)->color);

What I like to do in these situations is to get a local pointer and use that 在这种情况下,我想做的就是获取一个本地指针并使用它

void foo(void *v)    //v should be the pointer to the dequeued queueItem
{
    struct queueItem *localpointer = v;
    //do other stuff
    mix(localpointer->color);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM