C will automatically convert void *
to any pointer to object type and will automatically convert any unqualified pointer to object type to void *
, so you do not need to add casts and generally should not. (C++ is different in this regard.)
The fact that such conversions will be performed automatically does not mean the resulting values will be correct for any purpose. If the pointer is being converted back to a type of object it originally pointed to, that is fine. For example, if you pass a pointer to the first struct foo
of an array to the bsearch
routine and convert the pointer it returns back to struct foo *
, that is fine. If you convert it to int *
, that is a problem.
(In addition to converting a pointer back to its original type, you can convert any pointer to object type to char *
or unsigned char *
. This is allowed for the special purpose of manipulating the individual bytes that represent objects and should be avoided in most code.)
Note the above says “pointer to object type.” Pointers to functions are different and are not automatically converted back and forth between void *
and should not be converted to pointers to object types except in very special-purpose code (perhaps special situations in operating systems, when loading programs, dynamic linking, and so on).