C语言基础11
函数指针的定义:
函数类型 (标识符 指针变量名)(形参列表)
void printHello( );
void printHello( ){
printf("hello world!!! " );
}
main函数中:
//创建函数指针,同时赋值空.
int (*p)() = NULL;
p = printHello // 将函数名称 赋值给函数指针,相当于函数的入口赋值给一个内存地址.
p( ); // 此时函数指针p就可以像函数名一样调用. 就可以理解为p 于 函数名是"相等".
实用1:
int sumValue(int a , int b){
return a + b;
}
int subValue(int a , int b ){
return a - b;
}
main函数:
//创建一个函数指针变量
int (*p2)(int , int);
// char str[] ={0};
// 在堆空间开辟一个 1* 20 字节的空间,同时str指向控制台输入的字符数组.
char *str = malloc( sizeof(char) * 20);
printf("Please enter some thing :");
scanf("%s" , str); // 注意这里str本身就是指针,一个地址,不需要在用&或者地址,直接赋值.
if(strcmp(str, "sub") == 0){
p = subValue; // p 为函数指针,所以赋值给它的是函数的入口地址,就是函数名称 ,需要后面的( )
}else if( strcmp(str , "sum") == 0){
p = sumValue;
}else {
printf("you are wrong !! ");
}
int x = 20, y = 45;
int result = p( x, y ); 直接使用p来代替subValue或者sumValue函数名称,并调用函数.
printf("%d", result );
free( str );
str = NULL;
实用2:写一函数查找成绩90分以上的学员,使⽤用回调函数在姓名后加”高富帅".
typedef struct {
int num;
char name[30];
char gender;
float score;
} Student;
void printStudent(Student *stu, int count );
void printStudent(Student *stu, int count ){
for (int i = 0 ; i< count ; i++){
printf("%d %s %c %f ", (stu+i)->num, (stu+i)->name, (stu+i)->gender, (stu+i)->score);
}
}
void getStudent(Student *stu , int count , void (*p)(Student *));
void getStudent(Student *stu , int count , void (*p)(Student *)){
for(int i = 0 ; i< count ; i++){
if((stu+i)->score >= 90){
p(stu+i); //传入一个指针
}
}
}
void getName(Student *s1);
void getName(Student *s1){
strcat(s1->name, "白富美");
}
main函数:
Student stu[] ={
{21, "luoshuai", 'm', 76.8},
{12, "luoteng", 'f', 98.6},
{68, "liruoxuan", 'm', 29.6},
{43, "lihuahua", 'f', 99.7},
{87, "guagua", 'm', 23.5}
};
Student *st = stu;
int count = sizeof(stu) / sizeof(Student);
getStudent(st , count , getStudent);
printStudent(st, count);
//动态排序
为函数指针重新命名: 比如说 int (*p)(int ,int ) 在很多地方都可以用到,我觉得很长很丑,简化一下:
typedef int(*PFUNC)(int ,int ) 以后在程序中任何使用到该函数指针的地方都可以用PFUNC xxx 来创建函数指针变量.
仍然使用上述的结构体和结构体数组,我们根据age或者score或者name来进行排序
void sortStudent( Student *stu, int count);
void sortStudent( Student *stu, int count){
for(int i = 0 ; i< count -1; i++){
int minKey = i ;
for(int j = i+1; j< count; j++){
// 如果有一天我修改条件,根据姓名 或者我又修改,根据学号来排序,我们每次都需要过来修改一次很麻烦.
// 所以我们引入了动态排序的概率,不在需要,查询该函数的数据,只需要在外部进行不同条件的判断.
// if((stu+j)->score < (stu+minKey)->score ){
minKey = j;
}
}
if(minKey != i ){
Student temp = *(stu + i);
*(stu+i ) =*(stu +minKey);
*(stu+minKey) = temp;
}
}
}
main函数中:
Student *s1 = stu;
int count = sizeof(stu) /sizeof(Student);
// sortStudent(s1, count);
// printStudent(s1, count );
修改sortStudent()函数如下:
void sortStudent( Student *stu, int count, BOOL (*p2)(Student *, Student *));
void sortStudent( Student *stu, int count, BOOL (*p2)(Student *, Student *)){
for(int i = 0 ; i< count -1; i++){
int minKey = i ;
for(int j = i+1; j< count; j++){
// 如果有一天我修改条件,根据姓名 或者我又修改,根据学号来排序,我们每次都需要过来修改一次很麻烦.
// 所以我们引入了动态排序的概率,不在需要,查询该函数的数据,只需要在外部进行不同条件的判断.
if( p2(stu+j, stu+minKey) ){
minKey = j;
}
}
if(minKey != i ){
Student temp = *(stu + i);
*(stu+i ) =*(stu +minKey);
*(stu+minKey) = temp;
}
}
}
//根据名字来排序
BOOL changeStudentByName(Student *stu1, Student *stu2);
BOOL changeStudentByName(Student *stu1, Student *stu2){
return strcmp(stu1->name, stu2->name) > 0; // 注意这里需要大于0才能成立.
}
// 根据分数来排序
BOOL changeStudentByScore(Student *stu1, Student *stu2);
BOOL changeStudentByScore(Student *stu1, Student *stu2){
return stu1->score > stu2->score ; // 这里需要两两的分数大于或者小于才成立.
}
最后我们在main函数中调用:
Student *s1 = stu;
int count = sizeof(stu) /sizeof(Student);
sortStudent( stu, count, changeStudentByName );
printStudent(stu, count);
最后一个问题,返回值为函数指针:
typedef BOOL (*PFUNC)(Student *,Student *) ;
typedef struct {
char name[40];
PFUNC p;
} nameFunction;
PFUNC getFunctionName(char *str){
if(strcmp(str, "name") == 0 ){
return changeStudentByName;
}else if(strcmp(str, "num") == 0){
return changeStudentByNum; // 返回函数主入口的地址.表示为函数名称.
}else if(strcmp(str, "score") == 0){
return changeStudentByScore;
}
return NULL;
}
int str1[] ={0};
printf(" Please enter some thing s :");
scanf("%s", str1);
PFUNC p3 = getFunctionByName(str1);
if( p3 == NULL){
return 0;
}
sortStudent(st, count, p3);
printStudent(st, count);
C语言基础11的更多相关文章
- C语言基础(11)-随机数发生器
一. rand() rand是一个C语言库函数,功能是生成一个随机数.rand需要一个不同的种子,才能生成不同的随机数. 二. srand(int seed) rand需要一个不同的种子,才能生成不同 ...
- C语言基础 (11) 结构体 ,共用体 枚举 typedef
1 课堂回顾 作用域与生命周期 2 static 局部变量 2 打字游戏 3 内存分区代码分析 4 结构体基本操作 (复合类型[自定义类型 #include <stdio.h> #incl ...
- C++语言基础(11)-多态
一.产生背景 先看下面的例子: #include <iostream> using namespace std; //基类People class People{ public: Peop ...
- Java语言基础(11)
1 构造方法 构造方法是一种特殊的方法,只有在创建对象的时候才被调用,用来执行初始化的操作,比如给属性赋值... 1) 构造方法名字跟类名一致,没有返回值也就没有返回值类型 2) 格式: 类名(参数列 ...
- Java入门 - 语言基础 - 11.switch_case
原文地址:http://www.work100.net/training/java-switch-case.html 更多教程:光束云 - 免费课程 switch_case 序号 文内章节 视频 1 ...
- [11 Go语言基础-可变参数函数]
[11 Go语言基础-可变参数函数] 可变参数函数 什么是可变参数函数 可变参数函数是一种参数个数可变的函数. 语法 如果函数最后一个参数被记作 ...T ,这时函数可以接受任意个 T 类型参数作为最 ...
- GO学习-(11) Go语言基础之map
Go语言基础之map Go语言中提供的映射关系容器为map,其内部使用散列表(hash)实现. map map是一种无序的基于key-value的数据结构,Go语言中的map是引用类型,必须初始化才能 ...
- C语言基础回顾
第一章 C语言基础 1. C语言编译过程 预处理:宏替换.条件编译.头文件包含.特殊符号 编译.优化:翻译并优化成等价的中间代码表示或汇编代码 汇编:生成目标文件,及与源程序等效的目标的机器语言代码 ...
- 【GoLang】GO语言系列--002.GO语言基础
002.GO语言基础 1 参考资料 1.1 http://www.cnblogs.com/vimsk/archive/2012/11/03/2736179.html 1.2 https://githu ...
随机推荐
- 在函数中处理html点击事件在标签中增加属性值来解决问题。
- DOT + graphviz 轻松画图
一.简介DOT & graphviz1. DOT DOT是一种文本图形描述语言.DOT语言文件通常具有.gv或是.dot的文件扩展名.当然,在编写好.dot或者.gv的文件之后,需要有专 ...
- python3.4 伪装成浏览器获取页面信息失败
最近学了下网络爬虫,打算从一个网站上提取点东西,自己练练手,刚开始还从这个网站上取了正确的html,后来百般尝试还是不能取正确的html,希望能得到大家的帮助~ 我刚开始的代码是: 1 url=&qu ...
- css制作最简单导航栏
css制作最简单导航栏
- hdu 1337 The Drunk Jailer
http://acm.hdu.edu.cn/showproblem.php?pid=1337 #include <cstdio> #include <cstring> #def ...
- Windows SVN变化邮件通知(Python2.7实现)
1,新增文件post-commit.bat 内容: rem REPOS-PATH (the path to this repository) set REPOS=%1 rem REV (the num ...
- 【点击模型学习笔记】Predicting Clicks_Estimating the Click-Through Rate for New Ads_MS_www2007
概要: 微软研究院的人写的文章,提出用逻辑回归来解决ctr预估问题,是以后ctr的经典解决方式,经典文章. 详细内容: 名词: CPC -- cost per click CTR -- click t ...
- Java中基础类库使用
Java中基础类库: 在这里我仅仅介绍几种我个人觉得会常常使用的 1:Object类中的Clone机制仅仅是对对象进行浅层次的克隆,假设须要进行深层次的克隆的话那么就要自己写(详细Clone方法请參考 ...
- LoadRunner如何在注册业务脚本中设置参数化唯一性
LR在录制一个网站注册业务的脚本时,突然间遇到一个问题:注册时,由于注册用户需要验证唯一性,所以在LR回放脚本时,用Run-time Viewer工具回放可以发现(先在脚本中设置几个断点),真实运行的 ...
- LR如何利用siteScope监控MySQL性能
本次实验,是在自己的电脑上使用APMServ5.2.6部署Discuz2.X论坛下,对该论坛的数据库MySQL5.1进行性能测试的,下面讲述LoadRunner在设计场景时,如何利用siteScope ...