zd305 发表于 2013-10-9 15:30:41

函数指针问题请教!

本帖最后由 zd305 于 2013-10-9 15:33 编辑

环境:mplab x + xc8
下面代码 编译时 任务切换函数 警告:illegal conversion between pointer types,请教如何修改!
(原代码在51下 红色关键字为code 没有警告,)
//任务函数声明0-3
void task0(void);
void task1(void);
void task2(void);
void task3(void);
void ( * const task[ ])() = {task,task1,task2,task3};   //任务切换函数

void main(void) {
    unsigned char i;
    while(1){
      for(i=0;i<MAX_TASK;i++) if (task_delay==0) {run(task); break;}
    }
}

usingavr 发表于 2013-10-9 15:47:32

const去掉吧

monkerman 发表于 2013-10-9 16:14:57

本帖最后由 monkerman 于 2013-10-9 16:20 编辑


run 的参数加上 const 试试? 类型不匹配.
参考: 指针与const限定符

zd305 发表于 2013-10-9 17:21:24

void run(void(*ptask)())
{
   (*ptask)();
}
这是run函数

改为
void main(void) {
    unsigned char i;
    while(1){
      for(i=0;i<MAX_TASK;i++) if (task_delay==0) {run (const task); break;}
    }
}

报错

zd305 发表于 2013-10-9 18:57:34

const 去掉 同样 警告,下面 补充run函数

void run(void(*ptask)())
{
   (*ptask)();
}

kanprin 发表于 2013-10-9 19:55:51

我一般都是先定义函数指针类型:
typedef void(*vfunc_t)();

void task0(void);
void task1(void);
void task2(void);
void task3(void);

然后定义变量:
const vfunc_t task[] = {task1, task2, task3, task4};


void run(void(vfunc_t task)
{
   task();
}

ntkz 发表于 2013-10-9 20:09:18

typedef void (*call)(void);
const call tase = {a,b,c};

zd305 发表于 2013-10-9 20:20:25

查到错误
void ( * const task[ ])(void) = {task0,task1,task2,task3};   //任务切换函数
void run(void(*ptask)(void))
{
   (*ptask)();
}
如此修改(红色字符) OK
页: [1]
查看完整版本: 函数指针问题请教!