symbolads
symbolads

Reputation: 43

Getting the value inside a pointer pointed by another pointer?

I have a queue that stores thread structures

struct x
{
    int n;
    char *c;
    void *f;

};

The function below returns a structure defined as the following:

struct s {
    void *data;
};

When I call *new = allocate(&x_ptr), how can I access the members of x?

I tried the following but it does not work:

printf("%d\n", new ->data->n)

I get this error: request for member ‘n’ in something not a structure or union

Upvotes: 0

Views: 63

Answers (1)

0___________
0___________

Reputation: 67476

data has type of void *, you need to cast it to the correct pointer type (assuming it is referencing a valid object of that type).

printf("%d\n", node->data->threadid);

---->

printf("%d\n", ((threaddesc *)(node->data))->threadid);

Upvotes: 1

Related Questions