什么是(无效)'变量名'做在C函数的开始? [英] What does (void) 'variable name' do at the beginning of a C function?

查看:337
本文介绍了什么是(无效)'变量名'做在C函数的开始?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读这个样本code来自FUSE:

I am reading this sample code from FUSE:

<一个href=\"http://fuse.sourceforge.net/helloworld.html\">http://fuse.sourceforge.net/helloworld.html

和我有麻烦理解什么是code的片断所做的:

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;

具体而言,(无效)变量名的事情。我从来没有见过这种在C程序结构之前,所以我甚至不知道该怎么投入谷歌搜索框。我目前最好的猜测是,它是某种说明未使用的函数参数的?如果有谁知道这是什么,可以帮助我,那将是巨大的。谢谢!

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!

推荐答案

它的工作原理周围的一些编译器警告。一些编译器会发出警告,如果你不使用函数参数。在这种情况下,你可能会故意不使用该参数,无法更改界面出于某种原因,但还是要闭嘴的警告。这(无效)铸造结构是一个空操作,使警告消失。这里有一个简单的例子,用铿锵:

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:

int f1(int a, int b)
{
  (void)b;
  return a;
}

int f2(int a, int b)
{
  return a;
}

使用 -Wunused参数构建国旗和preSTO:

$ clang -Wunused-parameter   -c -o example.o example.c
example.c:7:19: warning: unused parameter 'b' [-Wunused-parameter]
int f2(int a, int b)
                  ^
1 warning generated.

这篇关于什么是(无效)'变量名'做在C函数的开始?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆