当前位置:网站首页>Advanced pointer (V) -- callback function
Advanced pointer (V) -- callback function
2022-07-18 15:07:00 【Yuan_ o_】
8、 Callback function
A callback function is a function called through a function pointer . If we put the pointer of the function ( Address ) Pass as argument to another function , When this pointer is used to call the function it points to , Let's just say this is a callback function . The callback function is not called directly by the function's implementer , It's called by another party when a particular event or condition occurs , Used to respond to the event or condition .
First, let's review bubble sorting :
// Bubble sort ( The first edition )
#include <stdio.h>
void bubble_sort(int arr[], int sz)
{
// Number of bubbling sortings
int i = 0;
for (i = 0; i < sz - 1; i++)
{
// A bubble sorting process
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;
}
Bubble sort defects of the first edition : If it's an ordered array , He will still compare one by one , A waste of time , Therefore, the following modifications are made :
// Bubble sort ( The second edition )
#include <stdio.h>
void bubble_sort(int arr[], int sz)
{
int i = 0;
// Number of bubbling sortings
for (i = 0; i < sz - 1; i++)
{
int flag = 1;// Suppose the array is well ordered
// A bubble sorting process
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;
}
The disadvantage of bubble sorting is that it can only sort integers .
stay C There is a library function in the language library qsort, It's a quick sort , It can sort any type of data .
qsort The data type of the function :
void qsort(void* base,// The starting position of the data to be sorted
size_t num,// The number of data elements to be sorted
size_t width,// The size of the data elements to be sorted ( Unit is byte )
int(__cdecl* compare)(const void* elem1, const void* elem2)// A function pointer - Comparison function
);
//__cdecl - Function calling convention ;compare - Compare the address of the function ;elem1、elem2 - They are the addresses of the two elements to be compared
Here we also need to know void * The pointer
int main()
{
int a = 10;
char* pa = &a;// here &a The type of int*, Report warning
void* pv = &a;//void* Is a pointer without a specific type , Can receive any type of address
//void* Is a pointer without a specific type , So you can't dereference , Also can not + - Integers
return 0;
}
qsort Use of functions :
#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()
{
// Test use qsort To sort integer data
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)
{
// Sort structure data by name
return strcmp(((struct Stu*)e1)->name , ((struct Stu*)e2)->name);
}
int cmp_stu_by_age(const void* e1, const void* e2)
{
// Sort structure data according to age
return (((struct Stu*)e1)->age - ((struct Stu*)e2)->age);
}
void test2()
{
// Test use qsort To sort structure data
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)// Exchange function
{
int i = 0;
for (i = 0; i < width; i++)// Exchange according to the actual width of the element , For example, two integer data , Exchange its eight bytes in turn
{
char tmp = *buf1;
*buf1 = *buf2;
*buf2 = tmp;
buf1++;
buf2++;
}
}
// according to qsort Function to transform the bubble sort function
//void bubble_sort(int arr[], int sz)// original edition
void bubble_sort(void* base, int sz, int width, int(*cmp)(const void* e1, const void* e2))// After reform
{
int i = 0;
// Number of bubbling sortings
for (i = 0; i < sz - 1; i++)
{
int flag = 1;// Suppose the array is well ordered
// A bubble sorting process
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)
// Here we need to pay attention to , First cast the type to char* type , Multiply by width
{
/*int ret = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = ret;*/
// In exchange for
Swap((char*)base + j * width, (char*)base + (j + 1) * width, width);
flag = 0;
}
}
if (flag == 1)
{
break;
}
}
}
void test3()
{
// Test and use the modified bubble_sort To sort integer data
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()
{
// Test and use the modified bubble_sort To sort structure data
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();// Test use qsort To sort integer data
//test2();// Test use qsort To sort structure data
//test3();// Test and use the modified bubble_sort To sort integer data
test4();// Test and use the modified bubble_sort To sort structure data
return 0;
}
边栏推荐
- Vulnhub-dc6 learning notes
- Constraints in vivado that you can't understand
- Wechat selection and voting of applet completion works applet graduation project (8) graduation project thesis template
- 小米手机安全卸载内置应用
- How should Amazon sellers prevent association? Don't cut the bill! (detailed explanation of evaluation self support number)
- Sudo cannot find the command command not found solution
- SLAM_ Rotational kinematics_ Relationship between velocity V and acceleration a in two coordinate systems
- Total sequencing problem
- 06 gulimall basic crud function creation
- Wechat Evaluation of applet design works Voting applet Graduation Design (5) Task Book
猜你喜欢

书写更自然,媲美原厂体验,南卡Pencil电容笔上手

STM32 application development practice tutorial: multi computer communication application development based on CAN bus

05 gulimall VirtualBox set fixed IP for virtual machine

C#使用Marshal.SizeOf计算结构体大小返回错误

Vulnhub-dc6 learning notes

「TakinTalks」_ 故障频繁发生,如何做好系统稳定性?

Operation of ES

Wechat selection and voting of finished works of applet graduation project (7) mid term inspection report

Vulnhub-DC7学习笔记

2022t elevator repair operation certificate examination questions and online simulation examination
随机推荐
JVM memory model
ES的操作
Select statement if else
AcWing 3540. Binary search tree binary sort tree BST
后台运行程序方法
2022 questions and answers of safety management personnel operation examination of hazardous chemical business units
Hybridclr -- epoch-making unity native C # hot update technology
【sdx62】SBL阶段读取GPIO的状态操作
2022t elevator repair operation certificate examination questions and online simulation examination
Will the arrears of Alibaba cloud international ECS be automatically released?
C library function - sscanf() usage
【成像】【7】太赫兹光学——光学元件和子系统
选择语句 if else
Constraints in vivado that you can't understand
【日常训练】515. 在每个树行中找最大值
德银天下港交所上市:市值39亿港元 陕汽集团是大股东
Writing a new app with swift5 requires some considerations
保研机试备考十四:BFS
HTAP (hybrid transaction and) of polardb for PostgreSQL
ZYNQ PL中断脉冲多久可以被CPU捕获到