问题
I am learning C. I need to define a function to type cast the value of void *
to the desired type. I'm not sure if I fully understand what I need to do. Here is my attempt. Can someone take a look and let me know if it's correct? If not, how should I fix it? Thank you in advance for your time.
void print_type(TYPE a)
{
void *v_ptr;
v_ptr = &a;
}
回答1:
In C, void *
is implicitly compatible with any data pointer type. If you have a POSIX implementation, then it's compatible with function pointers as well. There's no need for typecasting; conversely, it's even considered harmful.
来源:https://stackoverflow.com/questions/16449206/how-should-i-type-cast-void