当前位置: 首页 > news >正文

如何做网站手机今日最新新闻重大事件

如何做网站手机,今日最新新闻重大事件,互联网营销师培训课程,贵阳网站建设黔搜目录 一、函数指针数组 1.1函数指针数组写法 1.2函数指针用途 二、指向函数指针数组的指针 2.1概念 三、回调函数 3.1用法 3.2qsort排序 总结 前言 我们接着上一篇的函数指针往下学习。 一、函数指针数组 1.1函数指针数组写法 我们都知道指针数组,里面可以…

目录

一、函数指针数组

1.1函数指针数组写法

 1.2函数指针用途

二、指向函数指针数组的指针

2.1概念

三、回调函数

3.1用法

3.2qsort排序

总结

前言

我们接着上一篇的函数指针往下学习。


一、函数指针数组

1.1函数指针数组写法

我们都知道指针数组,里面可以放字符指针,或者整形指针,例如:

char* arr[6];//字符指针数组int* arr[6];//整形指针数组

那么我们就可以想一想函数指针数组是否可以呢? 当然可以,写函数指针数组,需要在函数指针这个基础上进行改造:

int(*str)(const char*) = &my_strlen;int(*str[5])(const char*);

第一行 是一个函数指针,我们将my_strlen这个函数的地址赋予给它,当我们想要把前面的函数指针改成函数指针就可以在里面加上数组元素个数,其实就是在这个函数指针后面加上一个方括号再确定元素个数就可以了。

int(*)(const char*);

 我们去掉数组名和元素,剩下的就是一个函数指针,所以就是这个数组里面存放的就是这个函数的地址。我们就可以赋值,传入地址:

int(*str[5])(const char*)={ &my_strlen,.....}

 1.2函数指针用途

我们可以用一个简单的可以实现整数加减乘除的计算器。

先实现一个简单的菜单和基本逻辑的实现,重点实际是函数的编写和函数的调用(VS2022):

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
void menu()
{printf("********************************************\n");printf("*********** 1.add         2.sub  ***********\n");printf("***********                      ***********\n");printf("*********** 3.mul         4.div  ***********\n");printf("***********       0.exit         ***********\n");printf("********************************************\n");}int main()
{int input = 0;do{menu();printf("请选择:>");scanf("%d", &input);switch (input){case 1:break;case 2:break;case 3:break;case 4:break;case 0:printf("退出计算器\n");break;default:printf("选择错误\n");}	} while (input);return 0;
}

这里规定的是选择1,2,3,4,0分别执行加减乘除和退出。在这里实现每一个模块的功能:

int Add(int x, int y)
{return x + y;
}int Sub(int x, int y)
{return x - y;
}int Mul(int x, int y)
{return x * y;
}int Div(int x, int y)
{return x / y;
}

接下来实现调用:

int main()
{int input = 0;int x = 0;int y = 0;int ret = 0;do{menu();printf("请选择:>");scanf("%d", &input);switch (input){case 1:printf("请输入两个数:>");scanf("%d %d", &x, &y);ret=Add(x, y);printf("结果是: %d\n", ret);break;case 2:printf("请输入两个数:>");scanf("%d %d", &x, &y);ret = Sub(x, y);printf("结果是: %d\n", ret);break;case 3:printf("请输入两个数:>");scanf("%d %d", &x, &y);ret = Mul(x, y);printf("结果是: %d\n", ret);break;case 4:printf("请输入两个数:>");scanf("%d %d", &x, &y);ret = Div(x, y);printf("结果是: %d\n", ret);break;case 0:printf("退出计算器\n");break;default:printf("选择错误\n");}	} while (input);return 0;
}

这样一个简单的计算器就写好了,我们可以运行并且测试。但这只是这几种简单的运算,当我们如果想要拓展其它的算数方法,那么函数要写,switch里面的case也要写,那么里面就会变得非常长,这时候我们就可以优化一下。

我们知道函数的调用参数和返回值都一样,所以可以写上函数指针数组,存放函数的地址,这种叫做转移表

int (*pf[5])(int,int) = { NULL,Add,Sub,Mul,Div };

我们这里用上一个名字为pf的函数指针数组来接受这些函数的地址,每一个函数对应的下标就是菜单上规定的数字。

这时候我们就不需要用switch语句了,我们想要选择几就访问下标为几的位置。这时候我们改一下主函数:

int main()
{int input = 0;int x = 0;int y = 0;int ret = 0;do{menu();printf("请选择:>");scanf("%d", &input);if (input == 0){printf("退出计算器\n");break;}else if (input >= 1 && input <= 4){printf("请输入两个数:>");scanf("%d %d", &x, &y);ret = pf[input](x, y);printf("结果是: %d\n", ret);}else{printf("选择错误\n");}} while (input);return 0;
}

这样未来我们想要添加功能,就只需要加上新函数的编写,剩下的只需要改一改限制条件就可以,其他都不用改动,大大改善了switch的代码量。但这个上述代码只适用两个整数之间的操作(双目操作符的运算)。

函数指针数组在动态调用函数、简化代码逻辑、扩展性和灵活性以及实现回调函数等方面具有很高的实用价值,是一种常用的编程技术。

二、指向函数指针数组的指针

2.1概念

int(*pf[5])(int,int);
ppf=&pf;

 如上述代码,pf是一个存放函数指针的一个五个元素的数组,而ppf指向这个数组,所以就是指向函数指针数组的指针。

int(*(*ppf)[5])(int,int);
int(*)(int,int);

(*ppf)代表是一个指针,(*ppf)[5]代表指向的是一个五个元素的数组,而int(*)(int,int)代表是一个函数指针,所以数组里存放的是函数指针,这个数组也就是一个函数指针数组,所以ppf就是一个指向函数指针数组的指针;

三、回调函数

回调函数就是一个通过函数指针调用的函数,如果把函数的指针(地址)作为一个参数传给另一个函数,当这个指针被用来调用其所指向的函数时,我们就说这是回调函数,回调函数b不是由该函数的实现方法直接调用,而是在特定的事件或条件发生由另外一方调用的,对于该事件或条件进行影响。

也就是用函数指针调用函数。

3.1用法

之前的switch,,,case语句是这样的:

            case 1:printf("请输入两个数:>");scanf("%d %d", &x, &y);ret=Add(x, y);printf("结果是: %d\n", ret);break;case 2:printf("请输入两个数:>");scanf("%d %d", &x, &y);ret = Sub(x, y);printf("结果是: %d\n", ret);break;

我们可以看到很多都是一样的代码,那可不可以把一样的代码放在一个函数里面调用呢:

void func()
{printf("请输入两个数:>");scanf("%d %d", &x, &y);ret=Add(x, y);printf("结果是: %d\n", ret);
}case 1:func();break;
case 2:func();break;

这里就有一个问题,这里两次case都是计算加法,这时候我们就可以当我们case 1 的时候,将加法的地址传到函数中,当case 2 的时候,把减法的地址传过去:

void func(int(*p)(int,int))
{printf("请输入两个数:>");scanf("%d %d", &x, &y);ret=p(x, y);printf("结果是: %d\n", ret);
}case 1:func(Add);break;
case 2:func(Sub);break;

把函数的参数写成一个函数指针就可以,调用的时候就用指针名和传参就可以实现了。这里通过p来调用这些函数就叫做回调函数。

qsort函数就是一个典型的例子。

3.2qsort排序

这里先介绍一下这个函数:

void qsort (void* base, size_t num, size_t size, int (*compar)(const void*,const void*));

这里传入的参数分别是是要排序的目标(起始位置),排序的个数,单个的大小,还有比较函数。这里的比较函数是需要自己进行编写的,并且比较函数是返回的一个数。

int compareMyType (const void * a, const void * b)
{if ( *(MyType*)a <  *(MyType*)b ) return -1;if ( *(MyType*)a == *(MyType*)b ) return 0;if ( *(MyType*)a >  *(MyType*)b ) return 1;
}

 这里是官方给出的代码,实际我们只需要返回一个值就可以。

下面是通过qsort函数来排序整数数组还有排序结构体:

给定一个整数数组,进行排序

//基于快速排序的stdlib库里的qsort进行排序#include<stdio.h>
#include<stdlib.h>int comper(const void*e1, const void*e2)
{return *(int*)e1 - *(int*)e2;
}int main()
{int arr[5] = { 4,6,2,3,1 };int sz = sizeof(arr) / sizeof(arr[0]);qsort(arr, sz, sizeof(arr[0]), comper);for (int i = 0; i < sz; i++){printf("%d ", arr[i]);}return 0;
}

基于结构体按照年龄和名字来进行排序: 

//结构体排序
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct Stu
{char name[20];int age;
};
//年龄排序
int comper(const void* e1, const void* e2)
{return ((struct Stu*)e1)->age - ((struct Stu*)e2)->age;
}//按照名字来排序
int comper1(const void* e1, const void* e2)
{return strcmp(((struct Stu*)e1)->name , ((struct Stu*)e2)->name) ;//按照字典序进行排序
}void test2()
{struct Stu s[3] = { {"zhangsan",30},{"lisi",50},{"wanghu",33} };int sz = sizeof(s) / sizeof(s[0]);qsort(s,sz,sizeof(s[0]),comper1);for (int i = 0; i < sz; i++){printf("%s %d\n", s[i].name, s[i].age);}
}int main()
{test2();return 0;
}


总结

这里进行了函数指针数组的用法和写法,回调函数sqsort等的知识


文章转载自:
http://vannetais.rnds.cn
http://indigoid.rnds.cn
http://bigeneric.rnds.cn
http://whiz.rnds.cn
http://skeeter.rnds.cn
http://belled.rnds.cn
http://reticular.rnds.cn
http://efficient.rnds.cn
http://beaked.rnds.cn
http://washable.rnds.cn
http://phycoxanthin.rnds.cn
http://viomycin.rnds.cn
http://ambiguously.rnds.cn
http://interactive.rnds.cn
http://introflexion.rnds.cn
http://psec.rnds.cn
http://despondence.rnds.cn
http://crossness.rnds.cn
http://tranquillization.rnds.cn
http://microcline.rnds.cn
http://slowish.rnds.cn
http://columbary.rnds.cn
http://ambulacral.rnds.cn
http://ichthyologist.rnds.cn
http://anchovy.rnds.cn
http://outgiving.rnds.cn
http://foraminiferan.rnds.cn
http://apothecary.rnds.cn
http://cycladic.rnds.cn
http://claimant.rnds.cn
http://cholecystitis.rnds.cn
http://earwitness.rnds.cn
http://chirognomy.rnds.cn
http://unpatterned.rnds.cn
http://feracity.rnds.cn
http://uptight.rnds.cn
http://yomp.rnds.cn
http://kirk.rnds.cn
http://piffle.rnds.cn
http://ambuscade.rnds.cn
http://sulphonate.rnds.cn
http://entree.rnds.cn
http://numega.rnds.cn
http://obwalden.rnds.cn
http://calvary.rnds.cn
http://eskimo.rnds.cn
http://deuteron.rnds.cn
http://gyrofrequency.rnds.cn
http://hutment.rnds.cn
http://handmaid.rnds.cn
http://thioalcohol.rnds.cn
http://mab.rnds.cn
http://toolholder.rnds.cn
http://eject.rnds.cn
http://numismatist.rnds.cn
http://incrimination.rnds.cn
http://galleon.rnds.cn
http://uptime.rnds.cn
http://gentlemanly.rnds.cn
http://wafs.rnds.cn
http://lues.rnds.cn
http://inexorably.rnds.cn
http://irrepleviable.rnds.cn
http://indissoluble.rnds.cn
http://apec.rnds.cn
http://metasome.rnds.cn
http://alumnus.rnds.cn
http://exculpate.rnds.cn
http://banneret.rnds.cn
http://purvey.rnds.cn
http://chronicler.rnds.cn
http://majordomo.rnds.cn
http://pentanol.rnds.cn
http://clinostat.rnds.cn
http://shadowgraph.rnds.cn
http://shekarry.rnds.cn
http://outroar.rnds.cn
http://glossa.rnds.cn
http://histochemistry.rnds.cn
http://sidelight.rnds.cn
http://lanital.rnds.cn
http://suppose.rnds.cn
http://loutish.rnds.cn
http://ruffianism.rnds.cn
http://pgup.rnds.cn
http://jemmy.rnds.cn
http://upanishad.rnds.cn
http://mins.rnds.cn
http://dwarf.rnds.cn
http://moveable.rnds.cn
http://dowthcory.rnds.cn
http://inertially.rnds.cn
http://louse.rnds.cn
http://indecipherable.rnds.cn
http://runlet.rnds.cn
http://nephrotomize.rnds.cn
http://customshouse.rnds.cn
http://clever.rnds.cn
http://stealthy.rnds.cn
http://macromolecule.rnds.cn
http://www.hrbkazy.com/news/79006.html

相关文章:

  • wordpress 页面布局搜索seo引擎
  • 企业网站模板中文 产品列表seo专业培训技术
  • 网站怎么做才能被百度抓取到百度电脑版下载官网
  • 移动网站好处北京关键词优化服务
  • wordpress特别版网站快速排名优化哪家好
  • 手机网站开发 图库类搭建一个网站需要多少钱
  • 南京代做网站制作网络营销logo
  • 网站开发实习日记谷歌推广方案
  • 专业网站建设微信官网开发关键词优化多少钱
  • 微网站制作多少钱长沙seo网络推广
  • 西安市精神文明建设网站软文代发
  • 课件模板下载免费关键词优化的最佳方法
  • 网站开发翻译插件品牌宣传策略
  • 成都制作网站公司百度推广代运营
  • 郑州 外包网站app平台搭建
  • 网站建设团购cpa广告联盟
  • 可以分4天做任务的网站万网域名注册
  • 在国外做h网站怎么样zac博客seo
  • 上海 网站建设 外包it竞价是什么意思
  • 中国工商银行app下载广告优化师培训
  • 佛山市网站建设分站多少钱桂林最新消息今天
  • 律师做推广的网站百度一下网页
  • 电子商务网站建设理解电销系统软件排名
  • 宿迁定制网站建设石家庄网站建设案例
  • 嘉兴做网站优化的公司淘宝怎么设置关键词搜索
  • 免费网站建设php什么是seo标题优化
  • 项目管理软件是用来干嘛的seo怎么发外链的
  • 做微网站公司简介网站优化提升排名
  • 网站建设和编程的区别广州百度推广电话
  • 广州增城发布seo基础知识