# 浅学指针（4）函数指针数组和qsort的使用

## 1.函数指针数组的⽤途

### 作用：可以让代码更简洁，逻辑更清晰

``````#include <stdio.h>
{
return a + b;
}
int sub(int a, int b)//减法
{
return a - b;
}
int main()
{
int x, y;
int input = 1;
int ret = 0;
do
{
printf("*************************n");
printf(" 0:exit 				 n");
printf("*************************n");
printf("请选择：");
scanf("%d", &input);
switch (input){
case 1:
printf("输⼊操作数：");
scanf("%d %d", &x, &y);
printf("ret = %dn", ret);
break;
case 2:
printf("输⼊操作数：");
scanf("%d %d", &x, &y);
ret = sub(x, y);
printf("ret = %dn", ret);
break;
default:
printf("选择错误n");
break;
}
} while (input);
return 0;
}
``````

``````#include <stdio.h>
{
return a + b;
}
int sub(int a, int b)
{
return a - b;
}
void calc(int(*pf)(int, int))
{
int ret = 0;
int x, y;
printf("输⼊操作数：");
scanf("%d %d", &x, &y);
ret = pf(x, y);
printf("ret = %dn", ret);
}
int main()
{
int x, y;
int input = 1;
int ret = 0;
int(*p[3])(int x, int y) = { 0, add, sub }; //转移表
do
{
printf("*************************n");
printf(" 0:exit 				 n");
printf("*************************n");
printf("请选择：");
scanf("%d", &input);
if ((input <= 4 && input >= 1)){
printf( "输⼊操作数：" );
scanf( "%d %d", &x, &y);
ret = (*p[input])(x, y);
printf( "ret = %dn", ret);
}
else if(input == 0)
{
printf("退出计算器n");
}
else
{
printf( "输⼊有误n" );
}
return 0;
}
``````

``````int(*p[3])(int x, int y) = { 0, add, sub };
``````

## 3 . qsort函数

### qsort函数可以排序所有数据类型

``````#include <stdio.h>
//qosrt函数的使⽤者得实现⼀个⽐较函数
int int_cmp(const void * p1, const void * p2)
{
return (*( int *)p1 - *(int *) p2);
}
int main()
{
int arr[] = { 1, 3, 5, 7, 9, 2, 4, 6, 8, 0 };
int i = 0;

qsort(arr, sizeof(arr) / sizeof(arr[0]), sizeof (int), 	int_cmp);
for (i = 0; i< sizeof(arr) / sizeof(arr[0]); i++)
{
printf( "%d ", arr[i]);
}
printf("n");
return 0;
}
``````

### 3. 1使⽤qsort排序结构体数据

``````struct Stu //学⽣
{
char name[20];//名字
int age;//年龄
};
//假设按照年龄来⽐较
int cmp_stu_by_age(const void* e1, const void* e2)
{
return ((struct Stu*)e1)->age - ((struct Stu*)e2)->age;
}
//strcmp - 是库函数，是专⻔⽤来⽐较两个字符串的⼤⼩的
//假设按照名字来⽐较
int cmp_stu_by_name(const void* e1, const void* e2)
{
return strcmp(((struct Stu*)e1)->name, ((struct Stu*)e2)->name);
}
//按照年龄来排序
void test2()
{
struct Stu s[] = { {"zhangsan", 20}, {"lisi", 30}, {"wangwu", 15} };
int sz = sizeof(s) / sizeof(s[0]);
qsort(s, sz, sizeof(s[0]), cmp_stu_by_age);
}
//按照名字来排序
void test3()
{
struct Stu s[] = { {"zhangsan", 20}, {"lisi", 30}, {"wangwu", 15} };
int sz = sizeof(s) / sizeof(s[0]);
qsort(s, sz, sizeof(s[0]), cmp_stu_by_name);
}
int main()
{
test2();
test3();
return 0;
}
``````

## 5 …qsort函数的模拟实现

### 补充：qsort可以排所有的数据类型，下面写的代码是qsort内部实现

``````#include<stdio.h>
int int_cmp(const void * p1, const void * p2)//比较函数跟qsort函数的比较函数一样
{
return (*( int *)p1 - *(int *) p2);
}
void _swap(void *p1, void * p2, int size)
{
int i = 0;
for (i = 0; i< size; i++)//因为不知道数据类型（void）所以两个元素选择一个一个字节交换
{
char tmp = *((char *)p1 + i);
*(( char *)p1 + i) = *((char *) p2 + i);
*(( char *)p2 + i) = tmp;
}
}
void bubble(void *base, int count , int size, int(*cmp )(void *, void *))
{
int i = 0;
int j = 0;
for (i = 0; i< count - 1; i++)
{
for (j = 0; j<count-i-1; j++){//和冒泡相似
if (cmp ((char *) base + j*size , (char *)base + (j + 1)*size) > 0)//cmp函数返回值是int，判断>0，如果成立，执行语句
{
_swap(( char *)base + j*size, (char *)base + (j + 1)*size, size);//size是宽度，不知道
}
}
}
}
int main()
{
int arr[] = { 1, 3, 5, 7, 9, 2, 4, 6, 8, 0 };
int i = 0;
bubble(arr, sizeof(arr) / sizeof(arr[0]), sizeof (int), int_cmp);
for (i = 0; i< sizeof(arr) / sizeof(arr[0]); i++)//打印
{
printf( "%d ", arr[i]);
}
printf("n");
return 0;
}
``````

THE END