来自this answer on Software Engineering, void 根据使用方式进行特殊处理。在C 和C++ 中,void 用于表示an absence of a data type,,而void * 用于表示pointer which points to some data/space in memory that does not have a type. void * 不能自行取消引用,必须先转换为另一种类型。这个强制转换不需要在C 中显式,但必须在C++ 中显式。 (这就是为什么我们不强制转换 malloc 的返回值,即void *。)
当与函数一起用作参数时,void 表示完全没有任何参数,并且是唯一允许的参数。尝试像变量类型一样使用 void 或包含其他参数会导致编译器错误:
int foo(void, int); //trying to use "void" as a parameter
int bar(void baz); //trying to use "void" as an argument's type
main.c:1:8: error: 'void' must be the first and only parameter if specified
int foo(void, int);
^
main.c:2:14: error: argument may not have 'void' type
int bar(void baz);
^
同样不可能声明类型为void的变量:
int main(void) {
void qux; //trying to create a variable with type void
}
main.c:5:8: error: variable has incomplete type 'void'
void qux;
void 作为函数的返回值表示不会返回任何数据。由于无法声明void 类型的变量,因此无法捕获void 函数的返回值,即使使用 void 指针。
void foo(int i) { return; }
int main(void) {
void *j;
j = foo(0);
return 0;
}
main.c:5:5: error: assigning to 'void *' from
incompatible type 'void'
j = foo(0);
^ ~~~~~~
无类型的void * 是另一种情况。 void 指针表示指向内存中某个位置的指针,但不表示该指针处的数据类型。 (这是used to achieve polymorphism in C,例如qsort() function。)但是,这些指针可能很难使用,因为很容易意外地将它们转换为错误的类型。下面的代码不会在C 中抛出任何编译器错误,但会导致未定义的行为:
#include <stdio.h>
int main(void) {
double foo = 47.2; //create a double
void *bar = &foo; //create a void pointer to that double
char *baz = bar; //create a char pointer from the void pointer, which
//is supposed to hold a double
fprintf(stdout, "%s\n", baz);
}
然而,下面的代码是完全合法的; casting to and from a void pointer never changes the value it holds.
#include <stdio.h>
int main(void) {
double foo = 47.2;
void *bar = &foo;
double *baz = bar;
fprintf(stdout, "%f\n", *baz);
}
47.200000
作为函数参数,void * 表示您传入的指针处的数据类型是未知的,这取决于您(程序员)来正确处理该内存位置的任何内容。作为返回值,void * 表示返回的数据类型未知或无类型,必须由程序处理。
int quux(void *); //a function that receives a pointer to data whose type is not known, and returns an int.
void *quuz(int); //a function that receives an int, and returns a pointer to data whose type is not known.
tl;dr 函数原型中的void表示“无数据”,表示无返回值或无参数,函数原型中的void *表示“该函数指针处的数据” is given 没有已知类型”,表示参数或返回值,其指针必须转换为不同的类型,然后才能使用指针处的数据。