C语言 警告:在将函数地址分配给函数指针时从不兼容的指针类型分配 [默认启用]
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27632217/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Warning: Assignment from Incompatible Pointer Type [enabled by default] while assigning Function Address to Function Pointer
提问by sAm
I am trying to implement a simple swap function using function pointer but when I assign the function's address to a function pointer:
我正在尝试使用函数指针实现一个简单的交换函数,但是当我将函数的地址分配给函数指针时:
`pointersTofunctionB.c:14:6:warning: assignment from incompatible pointer type [enabled by default].
`pointersTofunctionB.c:14:6:warning: 从不兼容的指针类型赋值[默认启用]。
This is my code:
这是我的代码:
#include <stdio.h>
void intSwap(int *a,int *b);
void charSwap(char *a,char *b);
void (*swap)(void*,void*);
int main(int argc, char const *argv[])
{
int a=20,b=15;
char c='j',d='H';
swap=&intSwap;// warning here
swap(&a,&b);
printf("%d %d\n",a,b);
swap=&charSwap;// warning here also
swap(&c,&d);
printf("%c %c\n",c,d );
return 0;
}
void intSwap(int *a,int *b)
{
*a=*a+*b;
*b=*a-*b;
*a=*a-*b;
}
void charSwap(char *a,char *b)
{
char temp;
temp=*a;
*a=*b;
*b=temp;
}
How can I solve this warning?
我该如何解决这个警告?
回答by Vlad from Moscow
The warnings appear due to the following quote from the C Standard
出现警告是由于 C 标准的以下引用
6.3.2.3 Pointers
6.3.2.3 指针
8 A pointer to a function of one type may be converted to a pointer to a function of another type and back again; the result shall compare equal to the original pointer. If a converted pointer is used to call a function whose type is not compatible with the referenced type, the behavior is undefined.
8 指向一种类型函数的指针可以转换为指向另一种类型函数的指针,然后再返回;结果应与原始指针相等。如果使用转换后的指针调用类型与引用类型不兼容的函数,则行为未定义。
That two functions would be compatible their parameters shall have compatible types
两个函数是兼容的,它们的参数应该有兼容的类型
6.7.6.3 Function declarators (including prototypes)
6.7.6.3 函数声明符(包括原型)
15 For two function types to be compatible, both shall specify compatible return types.146) Moreover, the parameter type lists, if both are present, shall agree in the number of parameters and in use of the ellipsis terminator; corresponding parameters shall have compatible types.
15 对于要兼容的两个函数类型,两者都应指定兼容的返回类型。146) 此外,如果两者都存在,参数类型列表应在参数数量和省略号终止符的使用方面达成一致;相应的参数应具有兼容的类型。
In your functions parameters are declared as pointers. So that they (pointers) would be compatible they shall be pointers to compatible types
在您的函数中,参数被声明为指针。为了使它们(指针)兼容,它们应该是指向兼容类型的指针
6.7.6.1 Pointer declarators
6.7.6.1 指针声明符
2 For two pointer types to be compatible, both shall be identically qualified and both shall be pointers to compatible types.
2 对于要兼容的两个指针类型,两者都应具有相同的限定,并且都应是指向兼容类型的指针。
However types int or char on the one hand and type void on the other hand are not compatible types.
然而,一方面 int 或 char 类型和另一方面 void 类型是不兼容的类型。
You could define your functions the following way
您可以通过以下方式定义您的功能
void intSwap( void *a, void *b )
{
int *x = a;
int *y = b;
*x = *x + *y;
*y = *x - *y;
*x = *x - *y;
}
void charSwap( void *a, void *b )
{
char *c1 = a;
char *c2 = b;
char temp = *c1;
*c1 = *c2;
*c2 = temp;
}
回答by Sourav Ghosh
You need to change
你需要改变
swap=&intSwap;
to
到
swap=intSwap;
Same goes for swap=&charSwap;also.
同去的swap=&charSwap;还有。
Again, your function signature(s) does not match the function pointer signature.
同样,您的函数签名与函数指针签名不匹配。
Your function is
你的功能是
void intSwap(int *a,int *b);
which is of return type void, two input parameters of int *, whereas, your function pointer signature is
其返回类型为 void,两个输入参数为int *,而您的函数指针签名为
void (*swap)(void*,void*);
which takes two void *s. Same for void charSwapfunction also.
这需要两个void *s。同样的void charSwap功能也。
Either yoou have to change the function signature, or you have to use a different function pointer prototype. Otherwise, the behaviour is undefined. [as mentioned in Vlad's answer].
您要么必须更改函数签名,要么必须使用不同的函数指针原型。否则,行为是未定义的。[如弗拉德的回答中所述]。
回答by Sujeet Kumar
#include<stdio.h>
using namespace std;
void intSwap(int *a,int *b);
void charSwap(char *a,char *b);
void (*swap)(int*,int*);
void (*swap1)(char*,char*);
int main()
{
int a=20,b=15;
char c='j',d='H';
swap=&intSwap;// warning here
swap(&a,&b);
printf("%d %d\n",a,b);
swap1=&charSwap;// warning here also
swap1(&c,&d);
printf("%c %c\n",c,d );
return 0;
}
void intSwap(int *a,int *b)
{
*a=*a+*b;
*b=*a-*b;
*a=*a-*b;
}
void charSwap(char *a,char *b)
{
char temp;
temp=*a;
*a=*b;
*b=temp;
}
**If you want you can use like it......**
回答by Sujeet Kumar
To remove the warning, code this overload:
要删除警告,请对此重载进行编码:
void (*swap)(int *,int*);
void (*swap)(char *,char*);

