Friday, January 20, 2017

c - Casting a void pointer to a struct



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



(struct data*)pointer


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


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...