函数指针的简单用法

 1 #include <studio.h>
 2 
 3 // compare function
 4 int jug(int x, int y)
 5 {
 6     if(x >= 0) return x;
 7     else if(y == 0) return x;
 8     else return x/y;             
 9 }
10 
11 // get sum
12 int sub(int x, int y)
13 {
14     return x+y;   
15 }
16 
17 // get sub
18 int minus(int x, int y)
19 {
20     return x-y;   
21 }
22 
23 void test(int (*p)(int, int), int a, int b)
24 {
25     int Int1;
26     Int1 = (*p)(a,b);
27     printf("a=%d, b=%d,  %d\n", a, b, Int1);   
28 }
29 
30 int main()
31 {
32     int a = 1, b = 2, c = 3, d = 4, e = 5;
33     test(sub, a, b);
34     test(minus, c, d);
35     test(jug, e, b);
36     return 0;
37 }

 

posted @ 2015-03-24 10:06  David_W  阅读(133)  评论(0编辑  收藏  举报