Detailed explanation of pointers in C language (6)

Table of contents
Key points of this chapter
1. Character pointer
2. Array pointer
3. Array of pointers
4. Array parameter passing and pointer parameter passing
5. Function pointer
6. Array of function pointers
7. Pointer to array of function pointers
8. Callback function
9. Analysis of pointer and array interview questions
pointer to array of function pointers
A pointer to an array of function pointers is a pointer pointer to an array whose elements are function pointers
Let's look at the code
by definition
//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);
The meaning of this code is that * first combined with () indicates that he is a pointer, and then combined with (int, int) indicates that he is a function pointer, and the return type of this function is Int type
Next we look at the callback function
So what is a callback function?
A callback function is a function that is called through a function pointer. If you pass a function pointer (address) as a parameter to another function, when this pointer is used to call the function it points to, we say this is a callback function. The callback function is not directly called , but is called by another party when a specific event or condition occurs, and is used to respond to the event or condition
Then let's look at the following code
#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;
}
So how do we optimize this code? ? ? Here we need to understand the qsort library function
qsort----quick sort quick sort
The qsort library function has 4 parameters. Here we focus on the last parameter. The * here is first combined with compar, indicating that it is a table pointer, and then combined with (const void*, const void*e2) to explain

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

 

 

 

 

Guess you like

Origin blog.csdn.net/fjj2397194209/article/details/131234585