c - C 中的 VOID 数据类型

标签 c type-conversion

如果我使用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/

相关文章:

c++ - cuda 将用户定义的结构传递给内核失败

c - 来自命令行参数的输入有效,但来自内部 char* 的输入给出总线错误。不应该是一样的吗?

c - 在c中添加char值

c - 为什么 GCC 不优化对 printf 的调用?

c++ - use-defined 转换为数组引用

java - 实现一个 kotlin 泛型函数,从复杂的 java 对象中检索特定字段

mysql - MySQL 中的 BLOB 到长文本转换

c++ - 如何读取字符或字符串并自动转换为数字(C/C++)?

c++ - 为什么 C++ 不允许 static_cast<function_type_of_f>(f)?

c++ - std::vector<uchar> 到 char* 数据丢失