简体   繁体   中英

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) ?

I tried casting (struct queueItem) v but that didn't work.

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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