函数指针
来源:互联网 发布:微盟 有赞 知乎 编辑:程序博客网 时间:2024/06/14 16:23
函数指针
刺猬@http://blog.csdn.net/littlehedgehog
在《自己动手》P212 代码5-58 有如下代码:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
来关注下第三个参数 t_pf_int_handler handler ,我们来看看 t_pf_int_handler 它的定义 (在 type.h 中)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
传说中的函数指针么。我不喜欢来啰哩啰嗦地大段砸概念,还是再来用一个实例看看究竟函数指针有什么用?
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
主要关注下这三个方面:
1、我们在代码开头就定义了一个函数指针类型,typedef void (*MyFuncPtr)(const char*) 而且这种类型似乎代表void,如果你觉得这个定义很碍眼,我们可以这样看看 typedef void Myfun ——用Myfun这个符号替代void 这样好理解了吧? 那上面那段代码不就是用 我们定义的函数指针类型代表void么
2、接下来我们定义了两个打印字符串的函数,hello()和shakehand() ,然后又是一个函数 void printmessage(MyFuncPtr ptr, const char* s) 我们先妥协下,暂时不管这个函数,看看主函数里面,我们定义了一个变量 MyFuncPtr pf ,请注意这个不是一个普通变量,而是一个指针,而且还是一个指向void这种函数的指针,即是我们指向的是一个void函数的入口地址,接下来的东西应该比较好懂了,我们先用pf这个函数的指针指向了hello这个函数,并且把这个指针作为参数传递给了printmessage 这个函数,此时我们再来看看printmessage 这个函数体吧 仅仅一句话 ——(*ptr)(s); 取得的函数指针加上了一个*号,这个表示它指向的函数实体了,也就是我们可以用hello这个函数替换掉(*ptr), 上述内容变为hello(s) ,这种形式就很好理解了吧~
我们再来回到原函数,按照上面的理解,t_pf_int_handler handler handler应该是一个指针,而且还是一个指向void类型函数的指针。我们来看看函数的调用,以验证我们的想法:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
第三个参数是一个函数名(代码如下),函数名就代表函数的入口地址,也可以理解为指向这个函数的指针了
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
divide_error 这个函数是用汇编写的,不过原理仍一样,表示这个函数的入口地址,其实汇编代码更清楚表示出了divide_error 这个函数名的实质。看看上面代码,不就是一个子程序开始的标号么?
最后想说下的是 typedef void (*MyFuncPtr)(const char*) 并没有定义函数 只是声明了一个类型而已!
- 指针函数 函数指针
- 函数指针/指针函数
- 指针函数&函数指针
- 函数指针 指针函数
- 函数指针 指针函数
- 指针函数,函数指针
- 函数指针,指针函数,
- 指针函数,函数指针
- 函数指针 指针函数
- 函数指针、指针函数
- 指针函数&函数指针
- 函数指针 指针函数
- 函数指针 指针函数
- 函数指针&指针函数
- 指针函数 函数指针
- 指针函数 &函数指针
- 函数指针&指针函数
- void指针 函数指针
- 淘宝小店---手机卡销售-----成功人士的选择
- 详解8步高级操作‖让笔记本速度快10倍‖陈峰教程
- Windows的消息机制
- jsp中光标焦点设置!
- 浪淘沙漫
- 函数指针
- 实现自己的操作系统--序
- 宽字符处理函数函数与普通函数对照表
- 从中关村到上地七街的走法
- 数据库应用系统的开发具体分为哪几个阶段
- 有关UNICODE、ANSI字符集和相关字符串操作的总结
- java常见笔试题目
- 在Visual Studio 2005的DataGridView控件中加入ComboBox下拉列表框的实现(C#)
- C语言程序静态库和动态库的创建及其应用