如果我使用void
声明任何变量,它会给出错误,但sizeof(void)
是1。
这是什么原因?
void var; // it gives error
printf("sizeof(void) = %d", sizeof(void)); //prints 1
最佳答案
sizeof (void)
在 C 中无效,但在 GNU C 中允许作为扩展以允许使用 void *
进行指针算术。
来自gcc
documentation :
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 a void or of a function as 1. [...] consequence of this is that sizeof is also allowed on void and on function types, and returns 1.
关于c - C 中的 VOID 数据类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28065199/