If I pass a void *vptr to a function which takes a other_type *ptr as its arg, will vptr be converted automatically to other_type *? Here is the code,
typedef struct A {
//...
}A;
void bar(A *a)
{
//do something with a
}
int main()
{
A a = {..};
void *vp = &a;
bar(vp); //will vp be converted to A*?
}
Is my code safe or correct?
Yes, void* is implicitly convertible to any pointer type, and any pointer type is implicitly convertible to void*. This is why you do not need to (and should not) cast the return value of malloc, for example.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With