sizeof(void) equals 1 in C? [duplicate]

2019-02-14 21:14发布

Possible Duplicate:
What is the size of void?

Hi all ! I am using gcc for compiling my C programs, just discovered accidentally that the sizeof(void) is 1 byte in C.

Is there any explanation for this ? I always thought it to be ZERO (if it really stores nothing) !

Thanks !

标签: c sizeof void
3条回答
太酷不给撩
3楼-- · 2019-02-14 21:21

Usually you don't ask for sizeof(void) since you never use void as type. I guess the behavior you are experimenting depends on the specific compiler. On my gcc it returns 1 as well.

查看更多
放我归山
4楼-- · 2019-02-14 21:27

This is a non standard extension of gcc, but has a rationale. When you do pointer arithmetic adding or removing one unit means adding or removing the object pointed to size. Thus defining sizeof(void) as 1 helps defining void* as a pointer to byte (untyped memory address). Otherwise you would have surprising behaviors using pointer arithmetic like p+1 == p when p is void*.

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

查看更多
登录 后发表回答