C中的多参数函数,其中从不使用第二个参数
我有两个function:
int hash64(int key0, int key1); int hash32(int key0, int UNUSED_ARG);
以及带有函数指针的结构
int (*hash)(int key0, int key1);
所以, hash32
只使用key0
,但是我需要以这种方式声明它以便将它分配给函数指针。 但是当我这样做时,GCC显然抱怨很多。 如果它是单参数函数,我们就可以做到
int hash1(int key); int hash2(void);
或者其他什么,但显然int hash32(int key0, void)
根据GCC是无效的语法。 那么,声明hash32的正确方法是什么?
您应该始终定义与您要使用函数的指针类型匹配的函数。
如果GCC抱怨未使用的参数,你可以用它来静音
int hash1(int key0, int key1) { (void)key1; ...
宣言看起来像这样
int hash32(int key0, int dummy);
并在函数定义中
int hash32(int key0, int dummy) { //... rest of code (void) dummy; // to avoid warnings for unused parameters. //.. return as required }