#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;
}
如果大家看完上面代码,没有疑问的话,就不用继续了,下面的链接对你没有太大帮助!