何时可以“void()”被使用和这样做的优点是什么 [英] When can "void()" be used and what are the advantages of doing so
问题描述
最近我开始学习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.
推荐答案
void
in C has three uses:
-
声明一个函数不返回值:
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!)要声明一个通用指针例如一个魔法饼干,或者在回调中交还给某人,那么就会被转换回原来的类型。
(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
被一个指向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.
这篇关于何时可以“void()”被使用和这样做的优点是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!