关于函数指针未知类型fntype为非 [英] Unknow type FNTYPE on function pointers

查看:138
本文介绍了关于函数指针未知类型fntype为非的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include<stdio.h>
#include<stdlib.h>

typedef void (*fn)(void) FNTYPE;
FNTYPE fn_arr[5];

void fun1(void){
        printf("\n I'm func 1 \n");
}

void fun2(void){
        printf("\n I'm func 2 \n");
}


fn_arr[0] = &fun1;
fn_arr[1] = &fun2;

int decidefunc(char* inp){
        if(inp == NULL){
                return 0;
        }
        else if(*inp == "a"){
                return 1;
        }
        else if(*inp == "b"){
                return 0;
        }
        else if(*inp == "c"){
                return 1;
        }
        else{
                return 0;
        }
}

void callMyFunc(char* inp){
        printf("\n %s \n",__func__);
        int idx = decidefunc(inp);
        fn_arr[idx]();
}

void do_lengthy_op(char* inp,void (*call)(char *inp)){
        printf("\n do_lengthy_operation! \n");
        call(inp);
}

int main(){
        do_lengthy_op("b",callMyFunc);
        return 0;
}

我收到上述错误有关回调简单的C程序。找不到原因的错误。

I'm getting the above error for the simple c programs on callback. Couldn't find why the error is.

推荐答案

应该固定下列问题后,工作。

It should work after fixing the following issues.


  1. 要定义一个指针到函数类型,则需要改用以下内容:

  1. To define a pointer-to-function type, you need to use the following instead:

typedef void (*FNTYPE)(void);


  • 要正确比较字符,您需要更改

    else if(*inp == "a"){
        return 1;
    }
    else if(*inp == "b"){
        return 0;
    }
    else if(*inp == "c"){
        return 1;
    }
    

    else if(*inp == 'a'){
        return 1;
    }
    else if(*inp == 'b'){
        return 0;
    }
    else if(*inp == 'c'){
        return 1;
    }
    


  • 将以下内容的main(),你不能拥有的 code函数外的。

    fn_arr[0] = &fun1;
    fn_arr[1] = &fun2;
    


  • 亲身体验: http://ideone.com/vbandN

    这篇关于关于函数指针未知类型fntype为非的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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