回调函数的范例———VS2010 下编译
来源:互联网 发布:淘宝的访客数和浏览量 编辑:程序博客网 时间:2024/06/05 20:41
1.回调函数范例一,在VS2010下编译通过
#include "stdafx.h"#include <stdio.h>void printWelcome(int len){ printf("欢迎欢迎 -- %d\n", len);} void printGoodbye(int len){ printf("送客送客 -- %d\n", len);} void callback(int times, void (* print)(int)){ int i; for (i = 0; i < times; ++i) { print(i); } printf("\n我不知道你是迎客还是送客!\n\n");}void main(void){ callback(10, printWelcome); callback(10, printGoodbye); printWelcome(5);}运行结果如下
2.回调函数的范例二,在VS2010 下编译,通过
#include <stdio.h>#include <conio.h> #include <stdlib.h>#include "stdafx.h"int my_strcmp(char* des, char* src);static void cmd_hello(void);static void cmd_hi(void);static void cmd_exit(void);//定义一个函数指针 typedef void (*callback)(void); //定义命令结构体 typedef struct cmd{ char name[10]; //命令的名字 callback func; //与命令相对应的函数指针 }cmd_t;//然后定义一个命令数组: //声明命令数组 cmd_t cmd_tbl[] = { {"hello",cmd_hello}, {"hi",cmd_hi}, {"exit",cmd_exit}, }; // 跟命令相对应的函数为: //相对应的功能函数为: void hello(void) { printf("hello\n"); } void hi(void) { printf("hi\n"); } static void cmd_hello(void) { hello(); } static void cmd_hi(void) { hi(); } static void cmd_exit(void) { //exit(0); } //此时我们还需要一个查找命令函数: cmd_t* my_find( char* val) { int i; for(i = 0; i < sizeof(cmd_tbl)/sizeof(cmd_tbl[0]); i++){ if(!my_strcmp(val,cmd_tbl[i].name)){ return &cmd_tbl[i]; } } return 0; } //字符串比较函数int my_strcmp(char* des, char* src){ while(*des){ if(*des != *src) return 1; des++; src++; } return *src - *des;}//此函数返回的是一个结构体指针,若没有找到命令则返回0; //main 函数如下: int main(void){ char val[20] = {}; cmd_t *cmd_ptr; while(1){ gets(val); cmd_ptr = my_find(val); if(cmd_ptr){ cmd_ptr->func(); } else{ printf("no cmd\n"); } } return 0; }运行结果:
0 0
- 回调函数的范例———VS2010 下编译
- 回调函数范例
- JavaScript回调函数的概念与范例
- 回调函数的原理及PHP范例
- 回调函数的实质——什么是回调函数,为什么要使用回调函数
- 回调函数的实质——什么是回调函数,为什么要使用回调函数
- php回调函数使用范例
- C++中回调函数的封装——实现。
- C++中回调函数的封装——使用
- 【C】——回调函数的好处
- 【C】——回调函数的好处
- 异步消息的传递—回调函数
- 委托——回调函数
- C语言——回调函数
- C++基础——回调函数
- C语言——回调函数
- JS笔记 —— 回调函数
- JavaScript——回调函数
- sharepoint 2013 在office 365中 如何嵌入JS代码实现搜索功能
- 关于Win7的一些小知识
- iOS技能培训的学习经验心得
- 黑马程序员-交通灯
- Linux下crontab命令的用法
- 回调函数的范例———VS2010 下编译
- iOS开发工具——统计Crash的工具Crashlytics
- 枚举使用
- document.domain 跨域问题
- cisco 路由器nat设置
- Jquery—JQuery对radio的操作(01)
- log4j输出多个自定义日志文件,动态配置路径
- Python核心编程学习笔记(二)
- ios 将NSLog日志重定向输出到文件中保存