深入学习指针(5)!!!!!!!!!!!!!!!

文章目录

1.回调函数是什么?

回调函数就是⼀个通过函数指针调⽤的函数。

如果你把函数的指针(地址)作为参数传递给另⼀个函数,当这个指针被⽤来调⽤其所指向的函数

时,被调⽤的函数就是回调函数。回调函数不是由该函数的实现⽅直接调⽤,⽽是在特定的事件或条

件发⽣时由另外的⼀⽅调⽤的,⽤于对该事件或条件进⾏响应

c 复制代码
#include <stdio.h>
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;
}
void cac(int (*pf)(int ,int) )//用所指向函数形式接收
{   
	int m, n;
	scanf("%d %d", &m, &n);
	int c=pf(m,n);
	printf("%d\n", c);
}
int main()
{
	int input;
	do {
		printf("****1.加法****\n");
		printf("****2.减法****\n");
		printf("****3.乘法****\n");
		printf("****4.除法****\n");
		printf("****0.退出****\n");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			cac(Add);
			break;
		case 2:
			cac(Sub);
			break;
		case 3:
			cac(Mul);
			break;
		case 4:
			cac(Div);
			break;
		case 0:
			break;
		default:
			printf("输入错误,请重新输入");
			break;
		}
	} while (input);
	
	return 0;
}

2.qsort使用举例

2.1使用qsort函数排序整形数据

c 复制代码
#include <stdio.h>
int paixu(const void* e1, const void* e2)
{
	return *(int*)e1 - *(int*)e2;
}
int main()
{   
	int arr[] = { 1,2,5,4,5,7,8,9,6,3 };
	qsort(arr, sizeof(arr) / sizeof(arr[0]), sizeof(arr[0]), paixu);
	int i = 0;
	for (i = 0;i < sizeof(arr)/sizeof(arr[0]);i++)
	{
		printf("%d ", arr[i]);
	}
	return 0;
}

2.2使用sqort排序结构数据

c 复制代码
#include <stdio.h>
#include <stdlib.h>
struct stu
{
	char name[20];
	int age;
};
int pum1(const void* e1, const void* e2)
{
	return strcmp(((struct stu*)e1)->name, ((struct stu*)e2)->name);
}
int main()
{   
	
	struct stu arr[3] = { {"zhangsan",15},{"lisi",19},{"wangwu",20} };
	qsort(arr, sizeof(arr) / sizeof(arr[0]), sizeof(arr[0]), pum1);
	
}

strcpm比较俩字符串大小,对应位置字符的ASCLL值。

按照年龄来排序,想从大到小排只需改变return两边的位置

c 复制代码
#include <stdio.h>
#include <stdlib.h>
struct stu
{
	char name[20];
	int age;
};

int pum2(const void* e1, const void* e2)
{
	return (*(struct stu*)e1).age - (*(struct stu*)e2).age;
}
int main()
{   
	
	struct stu arr[3] = { {"zhangsan",15},{"lisi",30},{"wangwu",20} };
	qsort(arr, sizeof(arr) / sizeof(arr[0]), sizeof(arr[0]), pum2);
	
}

3.qsort函数的模拟实现

相关推荐
云边散步3 分钟前
godot2D游戏教程系列二(4)
笔记·学习·游戏开发
skywalker_1125 分钟前
Java中异常
java·开发语言·异常
2501_9403152627 分钟前
航电oj:首字母变大写
开发语言·c++·算法
jrlong27 分钟前
DataWhale大模型基础与量化微调task4学习笔记(第 2 章:高级微调技术_RLHF 技术详解)
笔记·学习
没有天赋那就反复30 分钟前
JAVA 静态方法
java·开发语言
Darkershadow32 分钟前
蓝牙学习之Time Set
python·学习·蓝牙·ble·mesh
Thomas_YXQ43 分钟前
Unity3D在ios平台下内存的优化详解
开发语言·macos·ios·性能优化·cocoa
CodeByV1 小时前
【算法题】多源BFS
算法
好奇龙猫1 小时前
【日语学习-日语知识点小记-日本語体系構造-JLPT-N2前期阶段-第一阶段(9):単語文法】
学习
咸甜适中1 小时前
rust的docx-rs库,自定义docx模版批量生成docx文档(逐行注释)
开发语言·rust·docx·docx-rs