typedef 函数指针的用法
来源:互联网 发布:苹果网络恢复系统 编辑:程序博客网 时间:2024/06/07 05:20
typedef int (*MYFUN)(int, int);
上面的例子定义MYFUN 是一个函数指针,函数类型是带两个int 参数,返回一个int
在分析这种形式的定义的时候可以用下面的方法:
先去掉typedef 和别名,剩下的就是原变量的类型.
去掉typedef和MYFUN以后就剩:
int (*)(int, int)
下面的三个例子都是在vc6上编译通过。
例1:
#include <stdio.h>
char (*pFun)(int);
char glFun(int a)
{
printf("%d\n",a);
return 0;
}
void main()
{
pFun = glFun;
(*pFun)(2);
}
例2
#include <stdio.h>
typedef char (*PTRFUN)(int);
PTRFUN pFun;
char glFun(int a)
{
printf("%d\n",a);
return 0;
}
void main()
{
pFun = glFun;
(*pFun)(2);
}
例3:
#include <stdio.h>
typedef int (*FP_CALC)(int, int);
int add(int a, int b)
{
return a + b;
}
int sub(int a, int b)
{
return a - b;
}
FP_CALC calc_func(char op)
{
switch (op)
{
case '+': return add;
case '-': return sub;
default: return NULL;
}
return NULL;
//返回值为一个拥有两个int参数、返回类型为int的函数指针
int (*s_calc_func(char op)) (int, int)
{
return calc_func(op);
}
int calc(int a, int b, char op)
{
FP_CALC fp = calc_func(op); //根据预算符得到各种运算的函数的地址
//int (*fp)(int, int) = s_calc_func(op);//用于测试,等价于上一行
if (fp)
return fp(a, b); //根据上一步得到的函数的地址调用相应函数,并返回结果
else
return -1;
}
void main()
{
int a = 100, b = 20;
printf("calc(%d, %d, %c) = %d\n", a, b, '+', calc(a, b, '+'));
printf("calc(%d, %d, %c) = %d\n", a, b, '-', calc(a, b, '-'));
}
上面的例子定义MYFUN 是一个函数指针,函数类型是带两个int 参数,返回一个int
在分析这种形式的定义的时候可以用下面的方法:
先去掉typedef 和别名,剩下的就是原变量的类型.
去掉typedef和MYFUN以后就剩:
int (*)(int, int)
下面的三个例子都是在vc6上编译通过。
例1:
#include <stdio.h>
char (*pFun)(int);
char glFun(int a)
{
printf("%d\n",a);
return 0;
}
void main()
{
pFun = glFun;
(*pFun)(2);
}
例2
#include <stdio.h>
typedef char (*PTRFUN)(int);
PTRFUN pFun;
char glFun(int a)
{
printf("%d\n",a);
return 0;
}
void main()
{
pFun = glFun;
(*pFun)(2);
}
例3:
#include <stdio.h>
typedef int (*FP_CALC)(int, int);
int add(int a, int b)
{
return a + b;
}
int sub(int a, int b)
{
return a - b;
}
FP_CALC calc_func(char op)
{
switch (op)
{
case '+': return add;
case '-': return sub;
default: return NULL;
}
return NULL;
}
//s_calc_func为函数,它的参数是 op,//返回值为一个拥有两个int参数、返回类型为int的函数指针
int (*s_calc_func(char op)) (int, int)
{
return calc_func(op);
}
int calc(int a, int b, char op)
{
FP_CALC fp = calc_func(op); //根据预算符得到各种运算的函数的地址
//int (*fp)(int, int) = s_calc_func(op);//用于测试,等价于上一行
if (fp)
return fp(a, b); //根据上一步得到的函数的地址调用相应函数,并返回结果
else
return -1;
}
void main()
{
int a = 100, b = 20;
printf("calc(%d, %d, %c) = %d\n", a, b, '+', calc(a, b, '+'));
printf("calc(%d, %d, %c) = %d\n", a, b, '-', calc(a, b, '-'));
}
- typedef 函数指针的用法
- typedef 函数指针的用法
- typedef函数指针的用法
- typedef 函数指针的用法
- typedef 函数指针的用法
- typedef函数指针的用法
- typedef 函数指针的用法
- typedef函数指针的用法
- typedef 函数指针的用法
- typedef 函数指针的用法
- typedef 函数指针的用法
- typedef 函数指针的用法
- typedef 函数指针的用法
- typedef 函数指针的用法
- typedef函数指针的用法
- typedef 函数指针的用法
- typedef 函数指针的用法
- typedef 函数指针的用法
- Flex MD5加密类
- 动态创建Repeater模板
- pooling和旋转不变性
- 开机状态添加Linux网卡以及samba、sshd监听网卡设置
- RSA算法使用介绍
- typedef 函数指针的用法
- Qt 让窗口(或控件)居中
- 使用PS切片工具进行切片生成div布局的页面的方法以及如何确定图片中某个点在图片中的位置
- VS2010 mvc3.0学习入门-1
- Struts--ActionMapping作用介绍
- nagios启动报错的解决
- android camera默认参数设置
- Android make menuconfig make[1]: *** [scripts/kconfig/mconf] 错误 1
- Android设计模式系列-组合模式