函数指针举例

#include <stdio.h>
#include <stdlib.h>
typedef void (*funp_t)(char *sv);
funp_t funp;
funp_t funp_a;
funp_t funp_b;
void fun(char *sv)
{
    printf("%s\n", sv);
}

void fun_unknow(char *sv)
{
    printf("find unknow fun\n");
}

void case_a(char *sv)
{
    funp = &fun;
    funp("case_a\n");
}

void case_b(char *sv)
{
    funp = &fun;
    (*funp)("case_b\n");
}

void case_c(char *sv)
{
    funp = fun;
    funp("case_b\n");
}

void case_d(char *sv)
{
    funp = fun;
    funp_a = &fun;
    printf("fun ptr is these:\n");
    printf("fun:%p, funp:%p, funp_a:%p, funp_b:%p\n", fun, funp, funp_a, funp_b);
}

funp_t get_funp(int index)
{
    switch(index){
    case 'a':
        return case_a;
    case 'b':
        return case_b;
    case 'c':
        return case_c;
    case 'd':
        return case_d;
    default:
        return NULL;
    }
}

int main()
{
    int in;
    int runing = 1;
    funp_t funp_tmp;

    do{
        printf("Input a char of a b c d\n");
        in = getc(stdin);

        /*discard \n*/
        getc(stdin);

        funp_tmp = get_funp(in);

        if(funp_tmp)
            funp_tmp("");

        if(in == 'q')
            runing = 0;
    }while(runing);

    return 0;
}


如果大家看完上面代码,没有疑问的话,就不用继续了,下面的链接对你没有太大帮助!

http://blog.csdn.net/yangzhongxuan/article/details/7098132

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值