打印

关于keil编译器对指向函数的不解?望各位出手

[复制链接]
3524|7
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
cheungman|  楼主 | 2007-9-22 13:51 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
#include <stdio.h>
#include <at89x52.h>

static void Exchange1(int *first,int *second);
static void Process(int *m,int *n,void (*fun)(int,int));
static void PORT_UartInit(void);

static void Exchange1(int *first,int *second)
{
    int temp;
    temp=*first;
    *first=*second;
    *second=temp;
}

static void Process(int *m,int *n,void (*fun)(int,int))
{
    (*fun)(m,n);//此句有编译时候有警告
}

static void PORT_UartInit(void)
{
    SCON  = 0x50;              
    TMOD |= 0x20;             
    PCON |= 0x80;              
    TH1   = 0xff;                
    TL1   = 0xff;
    TR1   = 1;               
    TI    = 1;  
    ES    = 0;    
}

int main()
{
    int a=20,b=80;
    int *pt1,*pt2;
    void (*temp)();
    temp=Exchange1;
    PORT_UartInit();
    while(1)
    {    
            pt1=&a;
            pt2=&b;
        printf("num1=%d,num2=%d\n",*pt1,*pt2);
            Process(pt1,pt2,temp);
            printf("num1=%d,num2=%d\n",*pt1,*pt2);
    }
}

keil编译时候的警告:
POINTER FUNCTION.C(18): warning C214: 'Argument': conversion: pointer to non-pointer

我的疑惑:
程序有什么问题,出错在哪?


如果在WIN-TC 1.91中就可以顺利运行,难道keil对指针函数不支持?

#include <stdio.h>

static void Exchange1(int *first,int *second);
static void Process(int *m,int *n,void (*fun)(int,int));
static void PORT_UartInit(void);

static void Exchange1(int *first,int *second)
{
    int temp;
    temp=*first;
    *first=*second;
    *second=temp;
}

static void Process(int *m,int *n,void (*fun)())
{
    (*fun)(m,n);
}

int main()
{
    int a=20,b=80;
    int *pt1,*pt2;
    void (*temp)();
    temp=Exchange1;
    while(1)
    {    
            pt1=&a;
            pt2=&b;
            printf("num1=%d,num2=%d\n",*pt1,*pt2);
            Process(pt1,pt2,temp);
            printf("num1=%d,num2=%d\n",*pt1,*pt2);
    }
}

相关帖子

沙发
沈老| | 2007-9-22 16:57 | 只看该作者

应该是(*fun)(*m,*n);

函数定义是static void Process(int *m,int *n,void (*fun)(int,int));
其中m和n是指针(int*)怎么可以传给(*fun)他要求2个整数而不是2个整数指针.

使用特权

评论回复
板凳
cheungman|  楼主 | 2007-9-24 17:14 | 只看该作者

re 沈老

如果改为(*fun)(*m,*n),编译是成功的,但是调试发现结果达不到要求;

由于主函数在调用函数Process()时,其形参(*fun)()指向函数Exchange1(),所以在运行到语句(*fun)(m,n)时候,把其中的形参m,n的地址传递给实参pt1、pt2.

要是如果改为(*fun)(*m,*n),起不到交换目的了.

请教沈老跟各位,如何声明一个函数,其有指针形参.
比如有一个函数声明:
static void cheungman(int a,int b);
我可以这样声明为:
static void cheungman(int,int);

那么现在有这么一个函数:
static void cheungman(int *a,int *b);
那么又应该如何声明成跟上边一样啊.




使用特权

评论回复
地板
dalujia| | 2007-9-24 17:59 | 只看该作者

static void cheungman(int *,int *);

使用特权

评论回复
5
dalujia| | 2007-9-24 18:27 | 只看该作者

感觉还是楼主的对,(*fun)(m,n);

使用特权

评论回复
6
ayb_ice| | 2007-9-25 07:33 | 只看该作者

"(*fun)(m,n);"这里不对

(*fun) ((int)m, (int)n);

使用特权

评论回复
7
cheungman|  楼主 | 2007-9-25 14:03 | 只看该作者

re ayb_ice

不错,改成(*fun) ((int)m, (int)n)就对了;

这里是不是说把指针m,n的值当作一个整数取出来,再把这个整数值当作地址赋给另外两个指针pt1,pt2呢?

 

使用特权

评论回复
8
ayb_ice| | 2007-9-25 14:42 | 只看该作者

你这都是C的基本问题。。。

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

25

主题

255

帖子

1

粉丝