什么时候可以“void()”被使用以及这样做的好处是什么
我最近开始学习 C 语言,并注意到函数“void()”,但是,我想知道它的作用以及它的最佳应用点,也可能是 void 的替代方案,可能更高效。谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
没有名为
void
的函数,但可以声明返回类型为void
的函数。这意味着该函数不返回值。Update
void
还可用于向编译器指示该函数不带任何参数。例如,There is no function called
void
, but a function can be declared with a return type ofvoid
. This means that the function doesn't return a value.Update
void
can also be used to indicate to the compiler that the function does not take any arguments. For example,C 中的
void
有三种用途:声明函数不返回值:
声明函数不接受参数:
(危险将罗宾逊!)声明一个“通用指针”,它可以作为魔术饼干传递,或者传回给回调中的某个人,将被强制转换回其原始类型。
register_callback
使用指向 void 函数的指针进行调用,该函数需要一个可能对其有意义的指针作为参数。在将来的某个(未指定)时间,该函数将被调用,并以 bar 作为参数。您可以在某些类型的嵌入式执行程序和可重用设备驱动程序中看到这种情况,尽管在任何地方都不常见。void
in C has three uses:To declare that a function does not return a value:
To declare that a function does not accept parameters:
(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.
register_callback
is 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.当
void
作为函数参数有用时?在上面的代码片段中,尽管
foo()
原型没有参数,但向其传递某些内容仍然有效。因此,为了避免发生此类事情 -现在可以保证将任何内容传递给 foo() 都会生成编译器错误。
When
void
as function arguments is useful ?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 -Now it is guaranteed that passing anything to
foo()
would generate compiler errors.我认为没有 void() 函数。
但是,
void
关键字用于指示函数不返回任何内容,例如:I don't think there is a void() function.
However, the
void
keyword is used to indicate that a function doesn't return anything, e.g.: