C 函数参数用作语句
我看到了一段特定的代码C 函数参数用作语句,c,C,我看到了一段特定的代码 Int SensorxyzOpen(SensorHandle *handle_ptr, struct SensorCallback *client_callback_ptr, BOOL testcalibrated, void *contextp
Int SensorxyzOpen(SensorHandle *handle_ptr,
struct SensorCallback *client_callback_ptr,
BOOL testcalibrated,
void *contextptr)
{
(void)handle_ptr;
(void)client_callback_ptr;
(void)testcalibrated;
(void)contextptr;
return ERROR_NOT_SUPPORTED;
}
这个函数在做什么?函数参数如何用作语句这些语句没有实际效果。
(void)variable
这只是一种避免编译器抱怨未使用变量的方法。强制转换为void
这是一种以无害的方式引用变量的方法,即在不产生任何副作用的情况下,仍然可以使警告静音。
请注意,某些编译器可能会禁用变量not used警告并引发not used value警告,因为该值已被引用但未被使用
我见过人们也使用像自我分配这样的东西,比如variable1=variable1
另请注意,为了使特定变量的未使用变量警告静音,并且您正在使用
gcc
或clang
,您可以使用\uuuu属性((未使用))
装饰函数参数,如下例所示:
int foo (__attribute__((unused)) int no_warning, double raise_a_warning) {
return 0;
}
那些参考资料真的没什么用。它们可能是为了避免编译器发出“参数未使用”警告。