36-函数与指针
来源:互联网 发布:北航知行论坛 编辑:程序博客网 时间:2024/06/04 19:02
1、函数类型
2、函数指针
3、回调函数
使用c语言直接跳转到某个地址执行,这里就要用到回调函数
#include <stdio.h>typedef int(FUNC)(int);int test(int i){ return i * i;}void f(){ printf("Call f()...\n");}int main(){ FUNC* pt = test; void(*pf)() = &f; //等价void(*pf)() = 0x12345678 printf("pf = %p\n", pf); printf("f = %p\n", f); printf("&f = %p\n", &f); pf(); (*pf)(); printf("Function pointer call: %d\n", pt(2)); return 0;}
4、回调函数使用
#include <stdio.h>typedef int(*Weapon)(int);void fight(Weapon wp, int arg){ int result = 0; printf("Fight boss!\n"); result = wp(arg); printf("Boss loss: %d\n", result);}int knife(int n){ int ret = 0; int i = 0; for(i=0; i<n; i++) { printf("Knife attack: %d\n", 1); ret++; } return ret;}int sword(int n){ int ret = 0; int i = 0; for(i=0; i<n; i++) { printf("Sword attack: %d\n", 5); ret += 5; } return ret;}int gun(int n){ int ret = 0; int i = 0; for(i=0; i<n; i++) { printf("Gun attack: %d\n", 10); ret += 10; } return ret;}int main(){ fight(knife, 3); fight(sword, 4); fight(gun, 5); return 0;}
5、小结
lve/70/gravity/SouthEast)
阅读全文
0 0
- 36-函数与指针
- 指针函数与函数指针
- 指针函数与函数指针
- 函数指针与指针函数
- 指针函数与函数指针
- 函数指针与指针函数
- 函数指针与指针函数
- 函数指针与指针函数
- 函数指针与指针函数
- 函数指针 与 指针函数
- 函数指针与指针函数
- 函数指针与指针函数
- 函数指针与指针函数
- 函数指针与指针函数
- 函数指针与指针函数
- 函数指针与指针函数
- 函数指针与指针函数
- 函数指针与指针函数
- Android常用Support库
- 【面向JS--面向对象和原型】
- 关于Build Active Architecture Only属性
- ReactNative基础(六)使用react-navigation实现页面导航布局效果(TabNavigator)
- resultMap与resultType、parameterMap与 parameterType的区别
- 36-函数与指针
- Cmake Practice(二)
- 2017-10-13校内训练题题解
- mysql linux(centos6.5)环境安装----rpm方式
- HDU 2647 Reward
- 一款优秀的前端框架——AngularJS
- 在鼠标右键添加Edit with Sublime text
- WRF主程序与WPS的编译与安装
- java中”XX cannot be resolved to a type”的报错信息