function pointer demo in C language

#include 

int doAdd(int a, int b)   { return a + b; }
int doMinus(int a, int b) { return a - b; }
int test(int a,int b,int (*suck)(int,int)) {return suck(a,b);}

int main(void) {

int (*my_func_ptr)(int, int);

my_func_ptr = doAdd;
printf("function pointer to doAdd => %d
", (*my_func_ptr)(5, 3)); my_func_ptr = doMinus; printf("function pointer to doMinus => %d
", (*my_func_ptr)(5, 3)); //other method claim function pointer typedef int (*qq)(int,int); qq funcArray[1]; funcArray[0] = doAdd; funcArray[1] = &doMinus; printf("%d
" ,funcArray[0](5,3)); printf("%d
" ,funcArray[1](5,3)); int i=0; while(i<2) { printf("%d
" ,funcArray[i](5,3)); i++;}; printf("%d
",test(5,3,&doAdd)); printf("%d
",test(5,3,doMinus)); return 0; }
ref:http://caterpillar.onlyfun.net/Gossip/CGossip/FunctionPointer.html
http://www.newty.de/fpt/index.html

다음으로 전송:https://www.cnblogs.com/bittorrent/archive/2012/10/19/2730368.html

좋은 웹페이지 즐겨찾기