当前位置:网站首页>指针进阶(五)——回调函数
指针进阶(五)——回调函数
2022-07-16 06:44:00 【Yuan_o_】
8、回调函数
回调函数就是一个通过函数指针调用的函数。如果我们把函数的指针(地址)作为参数传递给另一个函数,当这个指针被用来调用其所指向的函数时,我们就说这是回调函数。回调函数不是由该函数的实现方直接调用,而是在特定的事件或条件发生时由另外的一方调用的,用于对该事件或条件进行响应。
首先回顾一下冒泡排序:
//冒泡排序(第一版)
#include <stdio.h>
void bubble_sort(int arr[], int sz)
{
//冒泡排序的趟数
int i = 0;
for (i = 0; i < sz - 1; i++)
{
//一趟冒泡排序的过程
int j = 0;
for (j = 0; j < sz - 1 - i; j++)
{
if (arr[j] > arr[j + 1])
{
int ret = arr[j];
arr[j] = arr[j + 1];
arr[j+1]=ret;
}
}
}
}
int main()
{
int arr[] = {
9,8,7,6,5,4,3,2,1,0 };
int sz = sizeof(arr) / sizeof(arr[0]);
bubble_sort(arr, sz);
int i = 0;
for (i = 0; i < sz; i++)
{
printf("%d ", arr[i]);
}
return 0;
}
冒泡排序第一版的缺陷:如果是排好序的数组,他依然会逐个比较,浪费时间,所以做出以下修改:
//冒泡排序(第二版)
#include <stdio.h>
void bubble_sort(int arr[], int sz)
{
int i = 0;
//冒泡排序的趟数
for (i = 0; i < sz - 1; i++)
{
int flag = 1;//假设数组排好序
//一趟冒泡排序的过程
int j = 0;
for (j = 0; j < sz - 1 - i; j++)
{
if (arr[j] > arr[j + 1])
{
int ret = arr[j];
arr[j] = arr[j + 1];
arr[j+1]=ret;
flag = 0;
}
}
if (flag == 1)
{
break;
}
}
}
int main()
{
int arr[] = {
9,8,7,6,5,4,3,2,1,0 };
int sz = sizeof(arr) / sizeof(arr[0]);
bubble_sort(arr, sz);
int i = 0;
for (i = 0; i < sz; i++)
{
printf("%d ", arr[i]);
}
return 0;
}
冒泡排序的缺点就是只能排序整型。
在C语言库里有一个库函数qsort,他是快速排序方式,他可以排序任意类型的数据。
qsort函数的数据类型:
void qsort(void* base,//要排序的数据的起始位置
size_t num,//待排序的数据元素的个数
size_t width,//待排序的数据元素的大小(单位是字节)
int(__cdecl* compare)(const void* elem1, const void* elem2)//函数指针-比较函数
);
//__cdecl - 函数调用约定;compare - 比较函数的地址;elem1、elem2 - 分别是需要比较的两个元素的地址
在这里我们还需了解一下void *指针
int main()
{
int a = 10;
char* pa = &a;//这里&a的类型为int*,会报警告
void* pv = &a;//void* 是无具体类型的指针,可以接收任意类型的地址
//void*是无具体类型的指针,所以不能解引用,也不能+ -整数
return 0;
}
qsort函数的使用:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int cmp_int(const void* e1, const void* e2)
{
return (*(int*)e1 - *(int*)e2);
}
void test1()
{
//测试使用qsort来排序整型数据
int arr[] = {
9,8,7,6,5,4,3,2,1,0 };
int sz = sizeof(arr) / sizeof(arr[0]);
qsort(arr, sz, sizeof(arr[0]), cmp_int);
int i = 0;
for (i = 0; i < sz; i++)
{
printf("%d ", arr[i]);
}
}
struct Stu
{
char name[20];
int age;
};
int cmp_stu_by_name(const void* e1, const void* e2)
{
//根据名字来排序结构体数据
return strcmp(((struct Stu*)e1)->name , ((struct Stu*)e2)->name);
}
int cmp_stu_by_age(const void* e1, const void* e2)
{
//根据年龄来排序结构体数据
return (((struct Stu*)e1)->age - ((struct Stu*)e2)->age);
}
void test2()
{
//测试使用qsort来排序结构体数据
struct Stu s[] = {
{
"zhangsan",15},{
"lisi",30},{
"wangwu",25} };
int sz = sizeof(s) / sizeof(s[0]);
qsort(s, sz, sizeof(s[0]), cmp_stu_by_name);
qsort(s, sz, sizeof(s[0]), cmp_stu_by_age);
}
void Swap(char* buf1, char* buf2, int width)//交换函数
{
int i = 0;
for (i = 0; i < width; i++)//根据元素实际宽度来进行交换,例如两个整型数据,依次交换其八个字节
{
char tmp = *buf1;
*buf1 = *buf2;
*buf2 = tmp;
buf1++;
buf2++;
}
}
//根据qsort函数来改造冒泡排序函数
//void bubble_sort(int arr[], int sz)//原版
void bubble_sort(void* base, int sz, int width, int(*cmp)(const void* e1, const void* e2))//改后
{
int i = 0;
//冒泡排序的趟数
for (i = 0; i < sz - 1; i++)
{
int flag = 1;//假设数组排好序
//一趟冒泡排序的过程
int j = 0;
for (j = 0; j < sz - 1 - i; j++)
{
//if (arr[j] > arr[j + 1])
if (cmp((char*)base + j * width, (char*)base + (j + 1) * width) > 0)
//这里需注意,先强制类型转换为char*类型,再乘宽度
{
/*int ret = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = ret;*/
//交换
Swap((char*)base + j * width, (char*)base + (j + 1) * width, width);
flag = 0;
}
}
if (flag == 1)
{
break;
}
}
}
void test3()
{
//测试使用改造后的bubble_sort来排序整型数据
int arr[] = {
9,8,7,6,5,4,3,2,1,0 };
int sz = sizeof(arr) / sizeof(arr[0]);
bubble_sort(arr, sz, sizeof(arr[0]), cmp_int);
int i = 0;
for (i = 0; i < sz; i++)
{
printf("%d ",arr[i]);
}
}
void test4()
{
//测试使用改造后的bubble_sort来排序结构体数据
struct Stu s[] = {
{
"zhangsan",15},{
"lisi",30},{
"wangwu",25} };
int sz = sizeof(s) / sizeof(s[0]);
//bubble_sort(s, sz, sizeof(s[0]), cmp_stu_by_name);
bubble_sort(s, sz, sizeof(s[0]), cmp_stu_by_age);
}
int main()
{
//test1();//测试使用qsort来排序整型数据
//test2();//测试使用qsort来排序结构体数据
//test3();//测试使用改造后的bubble_sort来排序整型数据
test4();//测试使用改造后的bubble_sort来排序结构体数据
return 0;
}
边栏推荐
- RMAN详细教程(一) —— 基本命令代码
- 03-GuliMall 开发环境配置
- Stonedb announces open source, why is the integrated real-time HTAP architecture the current best solution
- A series of questions about candy
- 86触摸开关/台扇/空调/智能家居/家电等,低功耗高抗干扰3键3路3通触摸IC-VK3603 ESOP8,性能稳定,灵敏度可调
- Function stack frame (worth collecting)
- AcWing 3540. Binary search tree binary sort tree BST
- Vulnhub-DC6學習筆記
- ZYNQ PL中断脉冲多久可以被CPU捕获到
- Preparation for Bao Yan machine test XIV: BFS
猜你喜欢

HMM & MEMM & CRF

小程序畢設作品之微信評選投票小程序畢業設計(5)任務書

MIPI C-PHY科普

How to insert, delete and obtain random elements with constant time

About mock third-party calls

Address problem when Xilinx FPGA starts configuration data from SPI flash

Configure maskrcnn environment roast (geforce mx250+win10+tensorflow1.5.0 GPU version)

SLAM_ Rotational kinematics_ Relationship between velocity V and acceleration a in two coordinate systems

Vulnhub-DC9学习笔记

2022t elevator repair operation certificate examination questions and online simulation examination
随机推荐
Wwdc22 - Apple privacy technology exploration
Establishment of APP automated test framework (VII) -- airtest basic operation
真值与条件表达式
Learning experience sharing 6: experience sharing of Dr. dry goods
Vulnhub-dc5学习笔记
【无标题】
CVPR 2022 | improve the utilization efficiency of small data sets, Fudan et al. Proposed layered cascaded vit network
Total sequencing problem
How to insert, delete and obtain random elements with constant time
86触摸开关/台扇/空调/智能家居/家电等,低功耗高抗干扰3键3路3通触摸IC-VK3603 ESOP8,性能稳定,灵敏度可调
A series of questions about candy
SLAM_ Rotational kinematics_ Relationship between velocity V and acceleration a in two coordinate systems
Vulnhub-DC8学习笔记
After 2000, he was hired as a special associate researcher of Nanjing University. He went to primary school at the age of 4 and was admitted to Nanjing University at the age of 14!
首届京东科技合作伙伴大会召开,博云携手京东科技共创产业数字化新增长
Opencv Library in pycharm cannot be automatically completed [July 2022]
选择语句 if else
Finding the median in data flow
watch时的value问题
STM32 application development practice tutorial: multi computer communication application development based on RS-485 bus