函数指针
2016-04-01 13:26
369 查看
形式:void (*fun_type) (int* a)
//例1,一个简单例子
//例2,函数类型定义后使用函数指针
//例3,回调函数,查找链表节点
//例1,一个简单例子
void f(int *a){ *a = 2333; } int main(){ void (*pf)(int *a) = &f; int a = 5; f(&a); //下面两种调用方法结果一致,因为编译器使用函数名时总是会将其转换为函数指针,第二种的操作多余 int b = 6; pf(&b); int c = 7; (*pf)(&c); int tt = 0; return 0; }
//例2,函数类型定义后使用函数指针
typedef void (*fun_type) (int* a); void fun(int *a){ *a = 2333; } int main(){ fun_type fun_instance = fun; //下面两种调用方法结果一致,因为编译器使用函数名时总是会将其转换为函数指针,第二种的操作多余 int a = 5; fun(&a); int b = 6; (*fun)(&b); int tt = 0; return 0; }
//例3,回调函数,查找链表节点
struct Node{ int the_value; Node *node_next; }; int compare_ints(void const *a, void const *b){ if (*(int *)a == *(int *)b) //强制类型转换 return 0; return 1; } Node *search_list(Node *node, void const *the_value, int (*compare)(void const *a, void const *b)){ while (node != NULL){ if (compare(&node->the_value, the_value) == 0) break; node = node->node_next; } return node; } int main(){ Node *first = new Node; Node *second = new Node; Node *third = new Node; first->the_value = 1; first->node_next = second; second->the_value = 2; second->node_next = third; third->the_value = 3; third->node_next = NULL; int desired_value = 2; Node *desired_node = search_list(first, &desired_value, compare_ints); int tt = 0; return 0; }
相关文章推荐
- file_get_contents()获取https出现这个错误Unable to find the wrapper “https”
- Ubuntu下U盘文件只读的解决办法
- spark1.6分布式集群环境搭建
- DB2行转列(多维度)
- Bitmap优化及内存优化
- 方法重载,递归,封装
- iOS开发-进阶:设置group类型tableview的section间距
- 在虚拟机中安装红旗桌面7.0 Linux操作系统的详细图文教程
- PHP实例函数:获取淘宝商品价格
- Codeforces VK Cup 2016 - Round 1 (Div. 2 Edition)
- uva 11806(容斥原理)
- mysql索引总结----mysql 索引类型以及创建
- 二维数组和指针
- 机器学习系列之EM算法
- 提高代码质量:如何编写函数 --转载
- 栈——链表实现
- 栈——链表实现
- 站群管理经验之Log日志监控
- iOS开发-UI控件:UITableView设置Section间距
- 跨服务器访问数据库