Sizeof (void) is 1 in C?

Possible duplicate:
What is the size of void?

Hello everybody! I use gcc to compile my C programs, I just accidentally discovered that sizeof (void) is 1 byte in C.

Is there any explanation for this? I always thought it was ZERO (if it really doesn't store anything)!

Thanks!

+4
source share
3 answers

This is not a standard gcc extension, but has a rationale. When you perform pointer arithmetic by adding or removing one unit, this means adding or removing an object that points to the size. Thus, defining sizeof(void) as 1 helps to define void* as a pointer to a byte (untyped memory address). Otherwise, you would have amazing behavior using pointer arithmetic, like p+1 == p , when p is void* .

The standard way would be to use `char * for this purpose (pointer to byte).

+6
source

Usually you do not request sizeof(void) as you never use void as a type. I assume that the behavior you are experimenting with is specific to the compiler. On my gcc, it also returns 1.

0
source

All Articles