tgun926
tgun926

Reputation: 1633

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.

Upvotes: 0

Views: 150

Answers (1)

pmg
pmg

Reputation: 108968

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

Upvotes: 5

Related Questions