The common folklore says that:
The type system exists for a reason. Integers and pointers are distinct types, casting between them is a malpractice in the majority of cases, may indicate a design error and should be avoided.
Even when such a cast is performed, no assumptions shall be made about the size of integers and pointers (casting void*
to int
is the simplest way to make the code fail on x64), and instead of int
one should use intptr_t
or uintptr_t
from stdint.h
.
Knowing that, when is it actually useful to perform such casts?
(Note: having a bit shorter code for the price of portability doesn't count as "actually useful".)
One case I know:
Anything more?