foo(void) 与 foo(void *)

foo(void) vs foo(void *)

从函数和语法上来说,原型为int foo(void)的函数和int foo(void *)的函数有区别吗?

我知道 int bar(int)int bar(int *) 之间的区别 - 其中一个正在寻找一个 int,另一个正在寻找一个 int 指针。 void 的行为是否相同?

From this answer on Software Engineering, void is treated specially depending on how it's used. In C and C++, void is used to indicate an absence of a data type, whereas void * is used to indicate a pointer which points to some data/space in memory that does not have a type. void * 不能自行取消引用,必须先转换为另一种类型。此转换在 C 中不必显式,但在 C++ 中必须显式。 (这就是为什么我们不强制转换 malloc 的 return 值,即 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 作为函数的 return 值表示不会 returned 数据。由于无法声明 void 类型的变量,因此无法捕获 void 函数的 return 值, 即使使用 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, such as with the 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 * 表示您传入的指针处的数据类型是未知的,由您(程序员)正确处理那里的任何内容内存位置。作为 return 值,void * 表示正在 returned 的数据类型未知或无类型,必须由程序处理。

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 在函数原型中表示 "no data" 并表示没有 return 值或没有参数,void * 在函数原型中表示 "the data at the pointer this function is given does not have a known type" 并指示参数或 return 值,其指针必须转换为不同类型才能使用指针处的数据。

foo(void) - 没有参数的函数

foo(void *) - 带有一个 void * 参数的函数

什么是void *?它只是指向没有指定类型的数据的指针。它可以转换为任何其他指针类型

unsigned add(void *arr)
{
   unsigned *uarr = arr;
   return uarr[0] + uarr[1];
}

Functionally and syntactically speaking, is there a difference between a function whose prototype is int foo(void) and int foo(void *)?

有区别:

int foo(void) 声明了一个不接受参数的函数。

int foo(void *) 声明了一个函数,该函数接受 void*.

类型的单个参数

在 C++ 中,int foo(void) 等同于 int foo()