C中的多参数函数,其中第二个参数从不使用 [英] A multi-argument function in C in which the second argument is never used

查看:162
本文介绍了C中的多参数函数,其中第二个参数从不使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个函数:

$ $ $ $ $ $ $ $> int hash64(int key0,int key1);
int hash32(int key0,int UNUSED_ARG);

以及一个带函数指针的结构

  int(* hash)(int key0,int key1); 

因此, hash32 只能使用 key0 ,但是我需要用这种方式声明它,以便将它分配给函数指针。但是,当我这样做时,GCC显然抱怨很多。如果是单参数函数,我们可以这样做:

$ p $ lt; code> int hash1(int key);
int hash2(void);

或其他,但显然是int hash32(int key0,void)根据GCC,code>是无效的语法。那么,声明hash32的正确方法是什么?

解决方案

您应该始终定义与您想要的指针类型匹配的函数使用你的函数。



如果GCC抱怨一个未使用的参数,你可以用

  int hash1(int key0,int key1)
{
(void)key1;
...


I have two functions:

int hash64(int key0, int key1);
int hash32(int key0, int UNUSED_ARG);

and a structure with a function pointer

int (*hash)(int key0, int key1);

So, hash32 only uses key0, but I need to declare it this way in order to assign it to the function pointer. But obviously GCC complains a lot when I do this. If it's a single-argument function, we can do

int hash1(int key);
int hash2(void);

or whatever, but apparently int hash32(int key0, void) isn't valid syntax according to GCC. So, what's the correct way to declare hash32?

解决方案

You should always define your function matching the pointer type with which you want to use your function.

If GCC complains about an unused parameter you can just silence it with

int hash1(int key0, int key1)
{
  (void)key1;
  ...

这篇关于C中的多参数函数,其中第二个参数从不使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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