Casting a void pointer to a struct

user1852050 picture user1852050 · Feb 18, 2013 · Viewed 74.5k times · Source

I started feeling comfortable with C and then I ran into type casting. If I have the following defined in an *.h file

struct data {
    int value;
    char *label;
};

and this in another *.h file

# define TYPE      void*

How do I cast the void pointer to the struct so that I can use a variable "TYPE val" that's passed into functions? For example, if I want to utilize the value that TYPE val points to, how do I cast it so that I can pass that value to another functions?

Answer

Alexey Frunze picture Alexey Frunze · Feb 18, 2013
(struct data*)pointer

will cast a pointer to void to a pointer to struct data.