函数指针数组小例子
在网上学到的,备忘:
[root@lex tst]# cat gao3.c #include <stdio.h> #include <string.h> char * function1 (char *p) { printf("In function1 %s\n",p); return p; } char * function2 (char *p) { printf("In function2 %s\n",p); return p; } char * function3 (char *p) { printf("In function3 %s\n",p); return p; } int main() { char * (*pf[3]) (char *p); pf[0] = function1; pf[1] = &function2; pf[2] = &function3; pf[0]("func1"); pf[1]("func2"); pf[2]("func3"); return 0; } [root@lex tst]#
执行:
[root@lex tst]# gcc -o gao3 gao3.c [root@lex tst]# ./gao3 In function1 func1 In function2 func2 In function3 func3 [root@lex tst]#