建设会员网站需要多少钱,微信公众平台推广,学院网站建设自评,wordpress文章外链回调函数传参#xff1a;函数指针做函数参数#xff08;回调函数#xff09; 目录背景回调函数是实现函数分层且单向依赖的好办法#xff0c;使用函数指针运行struct结构体回调函数代码更清晰简单理解回调函数Demo其他回调函数博文背景
这是我在实际工作中遇到的问题…回调函数传参函数指针做函数参数回调函数 目录背景回调函数是实现函数分层且单向依赖的好办法使用函数指针运行struct结构体回调函数代码更清晰简单理解回调函数Demo其他回调函数博文背景
这是我在实际工作中遇到的问题 线程创建 第三个参数 函数指针 完全不管具体实现什么功能把地址传过来完全分层黑盒子 涂鸦那个接口有我解析的代码也有mcu调用者的代码耦合在一起了 要完全分层双方代码要放在不同的 .c 文件里面互不干扰(比如各自OTA)。 回调函数是实现函数分层且单向依赖的好办法使用函数指针运行
回调函数是实现函数分层且单向依赖的好办法使用函数指针运行 视频讲解【回调函数和函数指针】干货分享彻底摆脱回调地狱 实际工作中很常见的一种方法是额外去写一个注册/安装函数 传递参数底层给上层传递参数 同时上层也可以给底层一个返回值实现数据双向的携带 总结
struct结构体回调函数代码更清晰 参考C/C struct 的回调函数使用技巧 地址https://blog.csdn.net/u010333084/article/details/51339469?spm1001.2014.3001.5502 回调函数是一个通过函数指针调用的函数。如果你把函数指针(函数的入口地址)传递给另一个函数当这个函数指针被用来调用它所指向的函数时我们就说这个函数是回调函数。可以作为函数参数传递哦。
使用struct 回调函数可以使得代码更加清晰明了.
1.定义回调函数的原型 typedef int STRU_FU;STRU_FU sfun1(int a, const char *b){printf(fun1 a %d , b %s \n,a,b);return a;
}STRU_FU sfun2(int a, void *b,float c){printf(fun2 a %d , b %f \n,a,c);return a;
}STRU_FU sfun3(int a, char b,long c){printf(fun3 a %d , *b %ld \n,a,c);return a;
}2. 定义的struct的回调函数
struct fun_ops{ //定义回调函数structSTRU_FU (*fun1)(int a, char *b); //注册参数STRU_FU (*fun2)(int a, void *b,float c);STRU_FU (*fun3)(int a, char b,long c);
};3. 注册回调函数
struct fun_ops fuops {//注册初始化函数指针的函数.fun1 sfun1,.fun2 sfun2,.fun3 sfun3,
};4.完整的代码
#include stdio.h
#include string.h
#include pthread.h
#include stdlib.htypedef int STRU_FU;STRU_FU sfun1(int a, const char *b){printf(fun1 a %d , b %s \n,a,b);return a;
}STRU_FU sfun2(int a, void *b,float c){printf(fun2 a %d , b %f \n,a,c);return a;
}STRU_FU sfun3(int a, char b,long c){printf(fun3 a %d , *b %ld \n,a,c);return a;
}struct fun_ops{ //定义回调函数structSTRU_FU (*fun1)(int a, char *b); //注册参数STRU_FU (*fun2)(int a, void *b,float c);STRU_FU (*fun3)(int a, char b,long c);
};struct fun_ops fuops {//初始化函数指针的函数.fun1 sfun1,.fun2 sfun2,.fun3 sfun3,
};int main(int argc, char* argv[])
{int g;struct fun_ops *ops NULL;if(argc 2){printf(please input 1 or 2 or 3 for test fun \n);return -1;}if(strcmp(argv[1], 1) 0){ops fuops;g 1;printf(fun1 \n);ops-fun1(111,fun1);//调用回调}else if( !strcmp(argv[1], 2)){ops fuops;g 2;printf(fun2 \n);ops-fun2(2222,fun2,3333);}else{ops fuops;g 3;printf(fun3 \n);ops-fun3(1212,y,9999999);}
}简单理解回调函数 你到一个商店买东西刚好你要的东西没有货于是你在店员那里留下了你的电话过了几天店里有货了店员就打了你的电话然后你接到电话后就到店里去取了货。
在这个例子里你的电话号码就叫回调函数你把电话留给店员就叫登记回调函数店里后来有货了叫做触发回调事件店员给你打电话叫做调用回调函数你到店里去取货叫做响应回调事件。
通过上面的例子我们有了以下几点概念 主函数相当于整个程序的引擎调度各个函数按序执行 回调函数一个独立的功能函数 中间函数(通用)一个介于主函数和回调函数之间的函数登记回调函数通知主函数起到一个桥梁的作用
在中间函数调用回调函数的时候我们需要把回调函数当作参数来传递那么就需要用到函数指针下面以一个例子来详细了解。
Demo
#include stdio.h//回调函数1 不同的调用者提供 类似多态
int callBack1(int x)
{return x*2;
}
//回调函数2 不同的调用者提供 类似多态
int callBack2(int x)
{return x/2;
}
//中间函数(通用) //函数指针
int calc(int b,int (* func)(int)) //func把函数当作参数来进行传递
{return 100func(b);
}int main()
{int a10;int qcalc(2,callBack1);//注册把函数赋值给函数指针的操作就是函数注册printf(%d\n,q);int pcalc(4,callBack2);//注册调用回调函数printf(%d\n,p);return 0;
}运行结果
分析一下代码流程 1、主函数需要调用回调函数 2、中间函数登记回调函数 3、触发回调函数事件 4、调用回调函数 5、响应回调事件
其他回调函数博文
c语言回调函数的使用及实际作用详解
C语言回调函数详解全网最全
回调函数是什么如何使用以及函数的注册
C语言 | 嵌入式重点知识之回调函数
在函数中调用函数有点象递归递归调用的是自己。