3 ms·
It has no type, so you can cast it to whatever it actually is. Even a function you will call. The main use case is generic functions. And in data structures li
by floam 2y ago
It has no type, so you can cast it to whatever it actually is. Even a function you will call.
The main use case is generic functions. And in data structures like this.
- shawn_w 2y agoCasting between void pointers and function pointers is, strictly speaking, undefined behavior. Though it does tend to work, it's not something to rely on. (And you don't need casts when converting between void pointers and data pointers)
- HexDecOctBin 2y agoAFAIK it's undefined behaviour in ISO C, but not so in POSIX.
- elteto 2y agoThe dlopen/dlsym API uses void *, there’s really no alternative in C. It might be undefined in the sense that a language lawyer might care, but for any sane implementation it’s completely valid.
- maccard 2y agoc11 fixed this. C++ allows casting as long as you don’t access the intermediate casted item and cast it back to the type it started as. In practice though, it works pretty much everywhere in my experience.