//int(*(*ppArr)[5])(int, int);
Look at this code, first combine with ppArr to indicate that ppArr is a pointer, and then combine with [5] to indicate that this is an array pointer, we remove (*paArr) [5] to see the effect
//int(*)(int, int);
#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 tmp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = tmp;
}
}
}
}
struct Stu
{
char name[20];
int age;
};
int main()
{
int arr[10] = { 9,8,7,6,5,4,3,2,1,0 };
int sz = sizeof(arr) / sizeof(arr[0]);
struct Stu s[3] = { {"zhangsan",20},{"lisi",30},{"wangwu",10} };
bubble_sort(arr, sz);//这个冒泡排序类型只能排序整型,不能排序其他类型
int i = 0;
for (i = 0; i < sz; i++)
{
printf("%d ", arr[i]);
}
return 0;
}
This is a function, this is a function, and the return type of the function is int. Let me explain to you what these four parameters represent.
base: The starting position of the target array
num: the size of the array (unit is element), that is, how many elements the array has
size: the size of the element (in bytes), that is, the size of an element (in bytes)
compar: compare
Now let's look at the code
#include<stdio.h>
#include<stdlib.h>
void qsort(void* base, size_t num, size_t size, int (*compar)(const void* e1, const void* e2));
int cmp_int(const void* e1, const void* e2)
{
}
int main()
{
int arr[10] = { 9,8,7,6,5,4,3,2,1,0 };
int sz = sizeof(arr) / sizeof(arr[0]);
//struct Stu s[3] = { {"zhangsan",20},{"lisi",30},{"wangwu",10} };//结构体数组
//bubble_sort(arr, sz);//这个冒泡排序类型只能排序整型,不能排序其他类型
qsort(arr, sz, sizeof(arr[0]), cmp_int);
int i = 0;
for (i = 0; i < sz; i++)
{
printf("%d ", arr[i]);
}
return 0;
}
We put the cmp_int function into the qsort function. The function name, parameter value and return type are consistent with the function parameters of the fourth parameter of qsort and the return value of the function, so we can find the cmp_int through (*compar) The function name (it can also be called the address of the function) you may have a question here, we have seen int*, char*, double*, float*, etc., but we have not seen void*, let me explain to you here, we look at the code
#include<stdio.h>
int main()
{
int a = 10;
int* pa = &a;
char* pc = &a;
void* p = &a;
p = &pc;
//void*类型的指针,可以接收任意类型的地址
}
A pointer of void* type, which can receive any type of address
When we want to sort other types (int*, char*, etc.), this is void to receive these types, so we don’t need to change the variable type many times
Let's take a look at the matters needing attention for the void* type, let's look at the code
#include<stdio.h>
int main()
{
int a = 10;
int* pa = &a;
char* pc = &a;
void* p = &a;
*p = 0;
p++;
//void*类型的指针,可以接收任意类型的地址
//void*类型的指针,不能进行解引用操作
//void*类型的指针,不能进行+-整数的操作
}
Let's look at the qsort function we wrote just now. The value of the void* pointer variable here cannot be changed (cannot be dereferenced). We need to force type conversion, and the forced type conversion to an int* type pointer can be dereferenced. up
Print out is an ascending array from small to large
#include<stdio.h>
#include<stdlib.h>
//void qsort(void* base, size_t num, size_t size, int (*compar)(const void* e1, const void* e2));
int cmp_int(const void* e1, const void* 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]);
//struct Stu s[3] = { {"zhangsan",20},{"lisi",30},{"wangwu",10} };//结构体数组
//bubble_sort(arr, sz);//这个冒泡排序类型只能排序整型,不能排序其他类型
qsort(arr, sz, sizeof(arr[0]), cmp_int);
int i = 0;
for (i = 0; i < sz; i++)
{
printf("%d ", arr[i]);
}
return 0;
}
I will give you a detailed explanation of the quick sort of the structure array and the quick sort of the float type at the beginning of the next blog!
At the end of this chapter, this chapter is very difficult, please digest and understand