警告:在将功能地址分配给功能指针时,从不兼容的指针类型进行分配[默认启用] [英] Warning: Assignment from Incompatible Pointer Type [enabled by default] while assigning Function Address to Function Pointer

查看:112
本文介绍了警告:在将功能地址分配给功能指针时,从不兼容的指针类型进行分配[默认启用]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用函数指针实现一个简单的交换函数,但是当我将函数的地址分配给函数指针时:

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:来自不兼容指针类型的赋值[默认启用].

`pointersTofunctionB.c:14:6:warning: assignment from incompatible pointer type [enabled by default].

这是我的代码:

#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标准中的以下引用

The warnings appear due to the following quote from the C Standard

6.3.2.3指针

8指向一种类型的函数的指针可能会转换为指向 另一种功能,然后再返回;结果应进行比较 等于原始指针. 如果转换后的指针用于 调用其类型与引用的类型不兼容的函数, 行为是不确定的.

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.

两个函数将兼容,它们的参数应具有兼容的类型

That two functions would be compatible their parameters shall have compatible types

6.7.6.3函数声明符(包括原型)

6.7.6.3 Function declarators (including prototypes)

15为使两种功能类型兼容,两者均应指定 兼容的返回类型.146)此外,参数类型列出(如果 两者都存在,应在参数数量和使用上达成一致 省略号的终止符; 相应参数应具有 兼容类型.

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.

在您的函数中,参数被声明为指针.为了使它们(指针)兼容,它们应该是指向兼容类型的指针

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指针声明符

6.7.6.1 Pointer declarators

2为使两种指针类型兼容,两种指针必须具有相同的限定,并且两种均应是指向兼容类型的指针.

2 For two pointer types to be compatible, both shall be identically qualified and both shall be pointers to compatible types.

但是一方面是int或char类型,而另一方面是void类型是不兼容的类型.

However types int or char on the one hand and type void on the other hand are not compatible types.

您可以通过以下方式定义函数

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;
}

这篇关于警告:在将功能地址分配给功能指针时,从不兼容的指针类型进行分配[默认启用]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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