C中的函数指针小结(读书笔记+自己写的测试代码)
来源:互联网 发布:ue4 2d 编程 编辑:程序博客网 时间:2024/05/17 03:31
《C和指针》这本书很实用,强烈推荐。
以下是关于书中高级声明,以及函数指针的应用,外加自己写的测试代码。
1. int *f()
按照符号的优先级,函数调用符()优先于*操作符,因此f是个函数,然后返回的int型指针;
2. int (*f) ()
加上括号后,*f先进行计算,也就是说f是一个函数指针,这个函数指针返回的类型是int;
3. int *(*f)()
此处的表达式是上述两者的整合,也即f是一个函数指针,并且返回int型整数;
下面考虑数组
4. int f[]
定义一个整型数组;
5. int *f[];
下标的优先级较高,因此首先f是一个数组,然后元素类型是指向整型的指针;
6. int f()[]
首先f是一个函数,其返回的是一个数组;其实函数是不能返回数组的,因此这里的声明是非法的;
7. int f[]()
首先f是一个数组,其元素类型为返回值为整型的数组;声明非法;
8. int (*f[ ])()
f是一个数组,其数组元素为返回值为int类型的函数指针;
9. int *(*f[ ])();
f是一个数组,其数组元素类型为返回值为int类型指针的函数指针;
10. 最后再来看下个C库函数中吓人的声明:
void (*signal( int sig, void(* handler)(int))) (int);
采用top_down的方式来看的话,就有:
void (*signal() ) (int);
首先signal是一个函数,外围的符号,可以看做其的返回,那么它的返回就是一个函数
指针,该函数的原型为:void f (int);
看明白这一层后,singal里面的两个参数就简单:一个int参数(信号类型),另外一个就是
信号处理函数指针,类似下面提到的回调函数。
有了上述的基础,我们来探讨函数指针的用途就显得十分方便,函数指针的主要用途有:
1. 回调函数;
2. 转移表;
废话少说,还是从代码入手吧:
<pre class="cpp" name="code">#include <iostream>using namespace std;typedef char* eleType;//typedef int eleType;typedef struct nodelist{eleType data;struct nodelist* next;}node;node* create(int n, const eleType begin){node* head = (node*)malloc(sizeof(node));head->next = NULL; head->data = begin;node* ph = head;for(eleType i=begin+1; i<begin+n; i++){node* p = (node*)malloc(sizeof(node));//p->data = i;p->data = "abc";ph->next = p;ph = p;ph->next = NULL;}return head;}void show(node* head){node* p = head;for(; p; p=p->next)cout<<p->data<<" ";cout<<endl;}//下面两个函数有不同的比较过程,如果我们要使用//同一个find函数框架的话?//假设里还有两种类型的node,其中一种里面数据类型//为char*,另外一种为int;//此处我们希望复用find的这个框架,那么//就可以使用回调函数;//后面如果有新的数据类型,也不用修改find框架,只需要//增加新的回调函数;node* find(node* head, const void* value, int (*compare)(const void* v1, const void* v2)){while(head!=NULL){if(compare(&head->data, value))break;head = head->next;}return head;}int compare_int(const void* v1, const void* v2){if(*(int*)v1 == *(int*)v2)return 1;return 0;}int compare_str(const void* s1, const void* s2){if(!strcmp(*(char**)s1, (char*)s2)) //使用指针的指针,因为&head->datareturn 1;return 0;}int main(){node* head = create(6, "10");//node* head = create(6, 10); //测试int版本show(head);//int a = 12;//node* t = find(head, &a, compare_int);//测试int版本;char* b = "abc";node* t = find(head, b, compare_str);if(t)printf("element found\n");elseprintf("no such element\n");system("pause");}
代码面前,了无秘密。大家运行调试下上面的代码就可以看懂流程。
上述的代码很好的说明”回调“二字的含义:
传入函数指针,在函数里面调用传进去的函数;
另外,上述的代码说明了回调函数在代码维护性和可重用性的好处。
下面的代码是说明转移表的例子:
#include <iostream>using namespace std;enum{ADD, SUB, MUL, DIV}op;int operation(int a, int b, int op){int result = 0;switch(op){case ADD:result = a + b;break;case SUB:result = a - b;break;case MUL:result= a * b;break;case DIV:result = a / b;break;default:;}return result;}inline int myadd(int a, int b){ //函数比较小,定义为inline类型;return a+b;}inline int mysub(int a, int b){return a-b;}inline int mymul(int a, int b){return a*b;}inline int mydiv(int a, int b){return a/b;}int main(){int a = 4;int b = 2;cout<<"the switch way: result = "<<operation(a, b, ADD)<<endl;int (*f[]) (int a, int b) = {myadd, mysub, mymul, mydiv}; //函数指针数组;cout<<"the function pointer table way: result = "<<f[0](a, b)<<endl;system("pause");return 0;}
与函数指针转移表形成对照的是switch语句,经过对比,可以发现的好处:
如果要添加新的函数,函数转移表只需要写个函数,更新下数组;
而switch方式则需要在代码里面添加新的语句,从维护和可扩展来说,较差;
- C中的函数指针小结(读书笔记+自己写的测试代码)
- [c#][测试]自己写的混淆代码
- C和指针 的读书笔记(自己整理)
- C语言中的指针(指针数组 数组指针 函数指针 函数指针数组 指向函数指针数组的指针)
- 几个自己写的字符串C函数
- 【积累】C/C++中明明该用函数实现的功能,为啥非要自己写代码!
- 自己写的智能指针(auto_ptr)
- 自己写一个strcmp函数(C++)
- C语言函数指针小结
- C语言函数指针小结
- 自己写的strcpy函数 mystrcpy()------指针版
- C语言中的指针小结
- C语言中的指针小结
- 自己写的cnCheckTreeView操作函数(数据库分级函数)测试通过(XP+D2009)
- 自己写的代码
- C语言的strcpy函数和自己写的函数
- c中的函数指针
- C中的函数指针
- android.os.NetworkOnMainThreadException
- css中li标签之列表项标志显示
- Android调用Jni,非常简单的一个Demo
- openwrt针对RT5350代码下载,配置和编译
- 判断字符串中A出现的个数,及单词的个数
- C中的函数指针小结(读书笔记+自己写的测试代码)
- 图片(js)
- 猜数字游戏
- vs2010编译notepad++源代码
- 有12个球,形状大小一样,有一个重量不一样,只能用天平称3次,找出那个球,应该怎么称?
- Archiva与Nexus配置
- 《Linux设备驱动开发详解》源码——globalmem
- 数学之路-分布式计算-linux/unix技术基础(3)
- 简单四则运算