When is casting void pointer needed in C?

后端 未结 5 1544
没有蜡笔的小新
没有蜡笔的小新 2020-12-05 13:56

I\'ve been looking at Advanced Linux Programming by Mitchell, Oldham and Samuel. I\'ve seen in the section on pthreads something about void pointers and casting tha

5条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-05 14:24

    No need to cast from or to a pointer to void in C:

    6.3.2.3 Pointers

    1 A pointer to void may be converted to or from a pointer to any incomplete or object type. A pointer to any incomplete or object type may be converted to a pointer to void and back again; the result shall compare equal to the original pointer.


    The only exceptions from this are

    • when printing a pointer using the "%p" conversion specifier as it's only defined for void *.
    • when copying a pointer's value from an intptr_t or uintptr_t back to a void *.

提交回复
热议问题