How to cast the address of a pointer generically while conforming to the C standard

前端 未结 5 2084
迷失自我
迷失自我 2020-12-17 19:00

It is common to assign pointers with allocations using an implicit function-return void * conversion, just like malloc()\'s:

void *malloc(size_t size);
int *         


        
5条回答
  •  星月不相逢
    2020-12-17 19:37

    I don't think it's possible to do it in a 100% standard-compliant manner, because non-void pointers are not guaranteed to have the strictly same size as a void*.

    It's the same reason the standard demands explicitly casting printf("%p") arguments to void*.

    Added: On the other hand, some implementations mandate that this work, such as Windows (which happily casts IUnknown** to void**).

提交回复
热议问题