【发布时间】:2014-12-24 05:58:58
【问题描述】:
我正在尝试使用函数指针实现一个简单的交换函数,但是当我将函数的地址分配给函数指针时:
`pointersTofunctionB.c:14:6:warning: 来自不兼容指针类型的赋值 [默认启用]。
这是我的代码:
#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;
}
我该如何解决这个警告?
【问题讨论】:
标签: c function pointers function-pointers