(void)“变量名”是什么意思?在C函数的开头做什么?
我正在阅读 FUSE 的示例代码:
http://fuse.sourceforge.net/helloworld.html
我正在难以理解以下代码片段的作用:
static int hello_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
off_t offset, struct fuse_file_info *fi)
{
(void) offset;
(void) fi;
具体来说,是(void)“变量名”。我以前从未在 C 程序中见过这种构造,所以我什至不知道该在 Google 搜索框中输入什么内容。我目前最好的猜测是它是未使用的函数参数的某种说明符?如果有人知道这是什么并且可以帮助我,那就太好了。谢谢!
I am reading this sample code from FUSE:
http://fuse.sourceforge.net/helloworld.html
And I am having trouble understanding what the following snippet of code does:
static int hello_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
off_t offset, struct fuse_file_info *fi)
{
(void) offset;
(void) fi;
Specifically, the (void) "variable name" thing. I have never seen this kind of construct in a C program before, so I don't even know what to put into the Google search box. My current best guess is that it is some kind of specifier for unused function parameters? If anyone knows what this is and could help me out, that would be great. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它可以解决一些编译器警告。如果您不使用函数参数,某些编译器会发出警告。在这种情况下,您可能故意不使用该参数,由于某种原因无法更改界面,但仍然想关闭警告。该
(void)
转换构造是一个无操作,可以使警告消失。这是一个使用 clang 的简单示例:使用
-Wunused-parameter
标志构建并立即执行:It works around some compiler warnings. Some compilers will warn if you don't use a function parameter. In such a case, you might have deliberately not used that parameter, not be able to change the interface for some reason, but still want to shut up the warning. That
(void)
casting construct is a no-op that makes the warning go away. Here's a simple example using clang:Build using the
-Wunused-parameter
flag and presto:就代码而言,它什么也没做。
它在这里告诉编译器这些变量(在这种情况下是参数)未被使用,以防止出现
-Wunused
警告。另一种方法是使用:
It does nothing, in terms of code.
It's here to tell the compiler that those variables (in that case parameters) are unused, to prevent the
-Wunused
warnings.Another way to do this is to use: