C/C++回掉函数
来源:互联网 发布:比斯利数据 编辑:程序博客网 时间:2024/05/17 10:05
参考:https://www.cnblogs.com/chenyuming507950417/archive/2012/01/02/2310114.html
#include <stdio.h>typedef void (*P)(void);void fun1(void){ printf("hello world\n");}void fun2(void){ printf("cbs\n");}int main(void){ P p; p = fun1; p(); p = fun2; p(); return 0;}
#include <stdio.h>typedef void (*P)(void);void printf_text(void){ printf("hello world\n");}void call_printf_text(P p){ p();}int main(void){ P p = printf_text; call_printf_text(p); return 0;}
#include <stdio.h>typedef void (*P)(const char *);void printf_text(const char *str){ printf("%s\n",str);}void call_printf_text(P p,const char *str){ p(str);}int main(void){ P p = printf_text; call_printf_text(p,"hello cbs"); return 0;}
#include <stdio.h>#include <string.h>/************上级的代码*************/typedef void (*P1)(const char *);typedef void (*P2)(const char *,int);typedef struct{ P1 p1; P2 p2;}Fun;void call(Fun *funs){ const char *str1 = "shun"; const char *str2 = "com"; int len = strlen(str2); funs->p1(str1);//运行时call funs->p2(str2,len);//运行时call}/***********************************//************程序员根据上级的typedef定义并实现自己的方法****************/void fun1(const char *str){ printf("%s\n",str);}void fun2(const char *str,int len){ printf("%s\n",str); printf("%d\n",len);}Fun funs = {fun1,fun2};/******************最后根据上级的结构体打包函数指针*********************/int main(void){ call(&funs); return 0;}
阅读全文
0 0
- C/C++回掉函数
- [C/C++]C函数
- c++/c数据库函数
- C++,C函数指针
- C语言函数 --C
- C/C++:函数指针
- 【c/c++】printf函数
- 【c/c++】memcpy()函数
- 【c/c++】assert()函数
- 【c/c++】sprintf()函数
- 【c/c++】memset()函数
- C/C++--虚函数
- c 函数
- C函数
- C 函数
- C函数
- 【C++】函数
- C 函数
- JVM 微解2
- python 处理图片像素点
- D. Swaps in Permutation【并查集+优先队列】
- postgres 行安全策略
- 32位和64位系统内核函数调用从ZwProtectVirtualMemory到NtProtectVirtualMemory
- C/C++回掉函数
- python调用jieba(结巴)分词 加入自定义词典和去停用词功能
- 字符串统计
- java正则表达式
- 深度优先搜索示例
- 【C0001-笔记】std::nothrow不抛异常置空指针
- [LeetCode] 3. Longest Substring Without Repeating Characters
- SQL数据库基本概念
- 拦截器类