I know that void*
can hold any pointer but I wonder also if with casting it can hold anything besides an array or a struct, i.e., primitives and unions. Is this a guarantee with C (and if so which version) or just implementation-specific or not even a thing?
I wonder also if with casting it can hold anything besides an array or a struct, i.e., primitives and unions. Is this a guarantee with C [...]?
No, it is not. As a counterexample, pointers are typically 32 bits wide in C implementations for 32-bit CPUs,* but C requires type long long int
to be at least 64 bits wide.** C places no upper bound on the size of any arithmetic type and no (explicit) lower bound on the size of any pointer type, so in principle, even short
might be wider than void *
.
*For example, GCC uses the pointer representation specified by the target application binary interface (ABI). On x86 Linux, that means the x86 System V ABI, which defines the size of an address as 4 bytes.
** Per paragraph 5.2.4.2.1/1 of the current C language standard, the maximum value representable by an object of type long long int
must be at least 9223372036854775807
, which is 263 - 1. If you include the sign bit, too, then it follows that a long long int
requires at least 64 bits.