C-函数指针
2016-05-04 00:00
369 查看
C中函数指针最重要的两个用途:
(1)作为另一个函数的参数
(2)转换表(jump table)
C语言中,()的优先级大于*,因此函数int *f()与int (*f)(),前者返回值为int型的指针,后者f为指向函数的指针。
编写一个简单的链表测试用例,测试函数指针作为另一个函数的参数的用途。
程序运行结果为:
使用到的函数指针有两个:void (*handle)(void *)和int (*compare)(const void *, const void *)
分别用于遍历时的数据处理以及结点数据比较。
第二种转移表的用途主要用于取代switch,定义int (*func[])(int, int)。其意义为元素函数指针的数组。
(1)作为另一个函数的参数
(2)转换表(jump table)
C语言中,()的优先级大于*,因此函数int *f()与int (*f)(),前者返回值为int型的指针,后者f为指向函数的指针。
编写一个简单的链表测试用例,测试函数指针作为另一个函数的参数的用途。
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <assert.h> typedef struct node{ void *data; struct node *next; }Node; typedef struct list{ Node *head; Node *tail; int list_len; }List; typedef struct test{ char *dest_addr; char *local_addr; int link_quality; }Test; int compare_str(const void *s1, const void *s2) { Test *a, *b; a = (Test *)s1; b = (Test *)s2; if (!memcmp(a, b, sizeof(Test))){ return 0; } return 1; } void handle(void * data) { Test *n; n = (Test *)data; printf("dest_addr: %s\n", n->dest_addr); printf("local_addr: %s\n", n->local_addr); printf("link_quality: %d\n\n", n->link_quality); } void list_init(List *list) { list->head = NULL; list->tail = NULL; list->list_len = 0; } void list_destroy(List *list, void(*destroy)(void *)) { list->list_len = 0; Node *n, *tmp; n = list->head; while (n){ tmp = n->next; if (destroy){ destroy(n->data); } free(n); n = tmp; } } void list_traverse(List *list, void(*handle)(void *)) { Node *p; p = list->head; while (p){ handle(p->data); p = p->next; } } Node* make_node(void *data) { Node *n; n = malloc(sizeof(struct node)); assert(n != NULL); n->next = NULL; n->data = data; return n; } void list_insert(List *list, void *data) { Node *n; n = make_node(data); if (list->head == NULL){ list->head = n; list->tail = n; n->next = NULL; } else{ list->tail->next = n; list->tail = n; n->next = NULL; } list->list_len++; } void* list_search(List *list, void *key, int(*compare)(const void *, const void *)) { int i = 1; Node *n; n = list->head; while (n){ if (!compare(n->data, key)){ printf("List Node Number: %d\n", i); return n->data; } n = n->next; i++; } return NULL; } void main() { Node *n; List *list; list = (List *)malloc(sizeof(List)); list_init(list); Test n1 = { "192.168.5.10", "192.168.5.12", 1}; Test n2 = { "192.168.5.20", "192.168.5.22", 2 }; Test n3 = { "192.168.5.30", "192.168.5.32", 3 }; Test nt = { "192.168.5.20", "192.168.5.22", 2 }; list_insert(list, &n1); list_insert(list, &n2); list_insert(list, &n3); printf("list_length: %d\n", list->list_len); list_traverse(list, handle); n = list_search(list, &nt, compare_str); if (n != NULL){ printf("Find List Node \n"); } list_destroy(list, 0); system("pause"); }
程序运行结果为:
使用到的函数指针有两个:void (*handle)(void *)和int (*compare)(const void *, const void *)
分别用于遍历时的数据处理以及结点数据比较。
第二种转移表的用途主要用于取代switch,定义int (*func[])(int, int)。其意义为元素函数指针的数组。
相关文章推荐
- logstash 启动方式
- linux启动进程的方式
- logstash注意事项
- kibana字段过滤功能(是某个字段过滤)
- 发送日志graphite格式
- Apache Commons 常用工具包
- 厦门巨游网络科技有限公司(HOTPOWER)承接游戏UI外包
- VR创业很理想的方向 残酷的现实
- 程序员注意了,这些技能会了友谊小船就翻不了
- SSH整合开发(一)
- 删除字符串中重复字符
- 【NOIP2010】关押罪犯 虚点并查集
- Linux基本命令练习(4)
- 如何保护你的class文件--class文件加密
- 使用freemarker生成word文档时特殊字符的处理
- SH_TwoMonth总结
- django使用celery学习记录
- a:link,a:visited,a:hover,a:active 分别是什么意思?
- 零基础微信开发入门指南
- 关于json和jsonp(json跨域)的一点分享与见解