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\n", (*my_func_ptr)(5, 3)); 
my_func_ptr = doMinus;
printf("function pointer to doMinus => %d\n", (*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\n" ,funcArray[0](5,3));
   printf("%d\n" ,funcArray[1](5,3));
   
   int i=0;
   while(i<2) { printf("%d\n" ,funcArray[i](5,3)); i++;};
   
   printf("%d\n",test(5,3,&doAdd));
   printf("%d\n",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

你可能感兴趣的:(function pointer demo in C language)