1 #include<stdio.h> 2 int add(int a,int b ) 3 { 4 return a + b; 5 } 6 int sub(int a,int b ) 7 { 8 return a - b; 9 } 10 int mul(int a,int b ) 11 { 12 return a * b; 13 } 14 int div(int a,int b ) 15 { 16 return a / b; 17 } 18 int calc(int x,int y, int (*funp)(int,int))//上面的加减乘除函数就是回调函数,可以被形参中函数指针funp直接调用 19 { 20 int value = 0; 21 value = funp(x, y); 22 return value; 23 } 24 int main(int argc, const char *argv[]) 25 { 26 int m = 20,n = 10; 27 int ret = 0; 28 29 ret = calc(m,n,add); 30 ret = calc(m,n,sub); 31 printf("%d\n", ret); 32 33 return 0; 34 }