当前位置:网站首页>Consolidate and review the pointers and callback functions pointing to the function pointer array
Consolidate and review the pointers and callback functions pointing to the function pointer array
2022-07-20 08:29:00 【Feverish CPU】
One 、 A pointer to an array of function pointers
That is, the pointer points to an array , The elements of the array are all function pointers .
void test(const char* abcdef)
{
printf("%s\n", abcdef);
}
int main()
{
void (*p)(const char* abcdef) = test;// A function pointer
void (*parr[6])(const char* abcdef);// Function pointer array
void (*(*parr)[6])(const char* abcdef);// A pointer to an array of function pointers
return 0;
}
Two 、 Callback function
That is, pass the address of the function as a parameter to another function , When this pointer is used to call the function it points to , Let's just say this is a callback function .
Let's talk about bubble sorting first :
#include<stdio.h>
void Bubble_sort(int* arr, int sz)
{
int i = 0;
for (i = 0; i < sz - 1; i++)// Bubble sort
{
int j = 0;
for (j = 0; j < sz - 1 - 0; j++)// Once in a bubble sort
{
if (arr[j] > arr[j + 1])
{
int tmp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = tmp;
}
}
}
}
int main()
{
int arr[10] = { 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;
}
qsort function , Sort any type of data :
int cmp(const* e1, const* e2)
{
return (*(int*)e1 - *(int*)e2);
}
int main()
{
int arr[10] = { 9,8,7,6,5,4,3,2,1,0 };
int sz = sizeof(arr) / sizeof(arr[0]);
//Bubble_sort(arr, sz);
qsort(arr, sz, sizeof(arr[0]), cmp);
int i = 0;
for (i = 0; i < sz; i++)
{
printf("%d ", arr[i]);
}
return 0;
}
#include<string.h>
struct stu
{
char name[20];
int age;
};
int cmp_age(const void* e1, const void* e2)
{
return (((struct stu*)e1)->age-((struct stu*)e2)->age);
}
void test2()
{
struct stu s[] = { { "xiaohong",12 }, { "xiaolan",13 }, { "xiaohua",20 } };
int sz = sizeof(s) / sizeof(s[0]);
qsort(s, sz, sizeof(s[0]), cmp_age);
}
int main()
{
//test1();
test2();
return 0;
}
边栏推荐
猜你喜欢
随机推荐
【YOLOv5实现玩手机检测】
Servlet入门
练习(2)创建一个集合,存放元素“1“,“$“,“2“,“$“,“3“,“$“,“4“
【学习笔记】Unreal(虚幻)4引擎入门(四)
枚举(enum)奇妙的使用、联合体(共用体union)对空间节省的巧妙
bgr与rgb相互转换
Yolov5 détection des oiseaux
Pytorch target detection data processing (II) extracting difficult samples, low AP samples
Share what you learned this week - detailed explanation of transformer model
【学习笔记】“STL演讲比赛流程管理系统”作业总结
C语言中的文件操作
STM32-点灯程序
什么是多线程
Openwrt manually installs the netdata plug-in
二阶及N阶最长公共子序列
pytorch 实现retinanet(三)loss的定义和训练
Win10中用VS2019编译live555
C语言中会节省空间的“位段“
Recursive backtracking - maze walking
Pytorch yolo5 training any training set