Possible Duplicate:
What is the size of void?
In §6.2.5.19, the prophets let us know that:
The
void
type comprises an empty set of values
Then why does sizeof(void)
yield 1, when 0 seems to suffice?
It is a gcc extension: http://gcc.gnu.org/onlinedocs/gcc-4.4.2/gcc/Pointer-Arith.html#Pointer-Arith
In GNU C, addition and subtraction operations are supported on pointers to
void
and on pointers to functions. This is done by treating the size of avoid
or of a function as 1.A consequence of this is that
sizeof
is also allowed onvoid
and on function types, and returns 1.The option
-Wpointer-arith
requests a warning if these extensions are used.
The reason why void
needs a size to perform such arithmetics is that ptr - ptr2
does not actually gives you the numeric difference of the addresses but the number of elements the two pointers are apart - and the size of an element pointed to by void *ptr
is sizeof(*ptr)
which is sizeof(void)
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With