#include <stdio.h>
int max(int x, int y)
{
return x > y ? x : y;
}
typedef int (*p)(int, int);
int main(void)
{
/* p 是函数指针 */
// int (* p)(int, int) = & max; // &可以省略
int a, b, c, d;
printf("请输入三个数字:");
scanf("%d %d %d", & a, & b, & c);
p max_p = max;
/* 与直接调用函数等价,d = max(max(a, b), c) */
d = max_p(max_p(a, b), c);
printf("最大的数字是: %d\n", d);
return 0;
}
#include <stdio.h>
int max(int x, int y)
{
return x > y ? x : y;
}
int main(void)
{
/* p 是函数指针 */
int (* p)(int, int) = & max; // &可以省略
int a, b, c, d;
printf("请输入三个数字:");
scanf("%d %d %d", & a, & b, & c);
/* 与直接调用函数等价,d = max(max(a, b), c) */
d = p(p(a, b), c);
printf("最大的数字是: %d\n", d);
return 0;
}
typedef定义的是一种类型,函数定义的是一个函数
因此typedef定义的类型具体化的时候,需要初始化赋值
函数定义的函数已经是属于具体化的函数
本文通过一个具体的示例介绍了如何使用函数指针来调用函数。示例中定义了一个求最大值的函数,并通过函数指针实现了对这个函数的间接调用。此外,还对比了typedef定义的类型与直接定义的函数之间的区别。
293

被折叠的 条评论
为什么被折叠?



