C语言 什么时候可以使用“void()”,这样做有什么好处
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5400125/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
When can "void()" be used and what are the advantages of doing so
提问by Dillon Chaffey
I started learning the C language recently, and have noted the function "void()", however, I would like to know what it does and it's best points of application, also perhaps an alternative to void that is potentially more productive. Thank you.
我最近开始学习 C 语言,并注意到了函数“void()”,但是,我想知道它的作用以及它的最佳应用点,也可能是 void 的替代方法,可能更有效率。谢谢你。
回答by Andrew Cooper
There is no function called void, but a function can be declared with a return type of void. This means that the function doesn't return a value.
没有被调用void的函数,但可以声明一个返回类型为 的函数void。这意味着该函数不返回值。
void DoSomething(...)
{
....
}
Update
更新
voidcan also be used to indicate to the compiler that the function does not take any arguments. For example,
void也可用于向编译器表明该函数不接受任何参数。例如,
float CalculatePi(void)
{
....
}
回答by John R. Strohm
voidin C has three uses:
void在 C 中有三种用途:
To declare that a function does not return a value:
void foo(int x);To declare that a function does not accept parameters:
int baz(void);(DANGER WILL ROBINSON!) To declare a "universal pointer", that may be passed around as e.g. a magic cookie, or handed back to someone in a callback, there to be cast back to its original type.
int register_callback(void (*foo)(void *baz), void *bar);
声明一个函数不返回值:
void foo(int x);声明一个函数不接受参数:
int baz(void);(危险将罗宾逊!)声明一个“通用指针”,它可以作为例如魔法饼干传递,或者在回调中交回给某人,在那里被强制转换回它的原始类型。
int register_callback(void (*foo)(void *baz), void *bar);
register_callbackis called with a pointer to a void function that expects as parameter a pointer that presumably means something to it. At some (unspecified) time in the future, that function will be called, with bar as the parameter. You see this kind of thing in certain kinds of embedded executives and reusable device drivers, although not so much anywhere.
register_callback使用指向 void 函数的指针调用该函数,该函数期望作为参数的指针可能对它有意义。在未来的某个(未指定)时间,该函数将被调用,以 bar 作为参数。您可以在某些类型的嵌入式管理人员和可重用设备驱动程序中看到这种情况,尽管在任何地方都没有那么多。
回答by Mahesh
When voidas function arguments is useful ?
什么时候void作为函数参数有用?
#include "stdlib.h"
#include "stdio.h"
void foo();
int main()
{
foo(5); // Passing 5 though foo has no arguments. Still it's valid.
return 0;
}
void foo()
{
printf("\n In foo \n") ;
}
In the above snippet, though foo()prototype has no arguments it is still valid to pass something to it. So, to avoid such things to happen -
在上面的代码片段中,尽管foo()原型没有参数,但向它传递一些东西仍然有效。所以,为了避免这样的事情发生——
void foo(void) ;
Now it is guaranteed that passing anything to foo()would generate compiler errors.
现在可以保证传递任何东西foo()都会产生编译器错误。
回答by Meta
I don't think there is a void() function.
我认为没有 void() 函数。
However, the voidkeyword is used to indicate that a function doesn't return anything, e.g.:
但是,void关键字用于表示函数不返回任何内容,例如:
void MyFunction() {
// function body here
}

