I have a situation where in the address inside the void pointer to be copied to a another pointer. Doing this without a type cast gives no warnings or errors. The pseudo code looks like
structA A;
void * p = &A;
structA * B = p;// Would like to conform this step
I don't foresee any problems with this. But since this operation is used over a lot of places, I would like to conform whether it can have any replications. Also is there any compiler dependency?
No, this is fine and 100% standard.
A void *
can be converted to and from any other data/object pointer without problems.
Note that data/object restriction: function pointers do not convert to/from void *
.
This is the reason why you shouldn't cast the return value of malloc()
, which returns a void *
.
In C a void *
is implicitly compatible with any pointer. That why you don't need to cast when eg passing pointers of any type to functions taking void *
arguments, or when assigning to pointer (still of any type) from function returning void *
(here malloc
is a good example).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.