什么时候可以“void()”被使用以及这样做的好处是什么

发布于 2024-10-25 17:42:46 字数 81 浏览 2 评论 0原文

我最近开始学习 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

温馨耳语 2024-11-01 17:42:46

没有名为 void 的函数,但可以声明返回类型为 void 的函数。这意味着该函数不返回值。

void DoSomething(...)
{
  ....
}

Update

void 还可用于向编译器指示该函数不带任何参数。例如,

float CalculatePi(void)
{
 ....
}

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 DoSomething(...)
{
  ....
}

Update

void can also be used to indicate to the compiler that the function does not take any arguments. For example,

float CalculatePi(void)
{
 ....
}
依 靠 2024-11-01 17:42:46

C 中的 void 有三种用途:

  1. 声明函数不返回值:

    void foo(int x);
    
  2. 声明函数不接受参数:

    int baz(void);
    
  3. (危险将罗宾逊!)声明一个“通用指针”,它可以作为魔术饼干传递,或者传回给回调中的某个人,将被强制转换回其原始类型。

    int register_callback(void (*foo)(void *baz), void *bar);
    

register_callback 使用指向 void 函数的指针进行调用,该函数需要一个可能对其有意义的指针作为参数。在将来的某个(未指定)时间,该函数将被调用,并以 bar 作为参数。您可以在某些类型的嵌入式执行程序和可重用设备驱动程序中看到这种情况,尽管在任何地方都不常见。

void in C has three uses:

  1. To declare that a function does not return a value:

    void foo(int x);
    
  2. To declare that a function does not accept parameters:

    int baz(void);
    
  3. (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);
    

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.

紫罗兰の梦幻 2024-11-01 17:42:46

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") ;
}

在上面的代码片段中,尽管 foo() 原型没有参数,但向其传递某些内容仍然有效。因此,为了避免发生此类事情 -

void foo(void) ;

现在可以保证将任何内容传递给 foo() 都会生成编译器错误。

When void as function arguments is useful ?

#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 -

void foo(void) ;

Now it is guaranteed that passing anything to foo() would generate compiler errors.

毁虫ゝ 2024-11-01 17:42:46

我认为没有 void() 函数。

但是,void 关键字用于指示函数不返回任何内容,例如:

void MyFunction() {
   // function body here
}

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.:

void MyFunction() {
   // function body here
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文