例子一:
int fun(int *a, int *b)
{
printf("%d%d\n", *a, *b);
int temp = *a;
*a = *b;
*b = temp;
printf("%d%d\n", *a, *b);
return 0;
}
int ftest(int *x, int *y, int (*pf)(int*, int*))
{
return (*pf)(x, y);
}
int main()
{
int t1 = 6, t2 = 5;
int *p1 = &t1;
int *p2 = &t2;
int(*pf)(int*, int*) = fun;
ftest(&t1, &t2, fun);
printf("%d%d", t1, t2);
}
案例2:
int fun(int *a, int *b)
{
printf("%d%d\n", *a, *b);
int temp = *a;
*a = *b;
*b = temp;
printf("%d%d\n", *a, *b);
return 0;
}
int sum(int *a, int *b)
{
printf("%d+%d=%d\n", *a, *b,*a + *b);
return 0;
}
int ftest(int *x, int *y, int (*pf)(int*, int*))
{
return (*pf)(x, y);
}
int main()
{
int t1 = 6, t2 = 5;
int *p1 = &t1;
int *p2 = &t2;
int(*pf)(int*, int*) = fun;
ftest(&t1, &t2, fun);
ftest(&t1, &t2, sum);
printf("%d%d", t1, t2);
}