C language array pointer and function pointer

array pointer

For example: int (*p)[10];

//Explanation: p is first combined with *, indicating that p is a pointer variable, and then points to an array with a size of 10 integers, so p is a pointer, pointing to an array is called an array pointer. Note: The priority of [] is greater than *, so it is called array pointer
& array name and array name What is the difference:
the array name represents the address of the first element of the
array & the array name represents the address of the array, not the address of the first element of the array
For example :

#include<stdio.h>
#include<windows.h>
int main(){
	int arr[10] = { 0 };
	printf("arr=%p\n", arr);
	printf("&arr=%p\n", &arr);
	printf("arr+1=%p\n", arr + 1);
	printf("&arr+1=%p\n", &arr + 1);
	system("pause");
	return 0;
}

The result is:
insert image description here
It can be seen from the result that the address of the array +1 is to skip the size of the entire array (the integer type is 4 bytes) and the difference between &arr+1 is 40 relative to &arr (because it is int arr[10] ) is 4*10=40.
Determine what the following code means

int arr[5];        //整型数组
int *parr1[10];      //指针数组
int (*parr2)[10];     //数组指针
int (*parr3[10])[5];   //里面放数组指针的数组

function pointer

void print(int x){
	printf("hello %d",x);
}
int main(){
	void (*pfun)(int)=print;
	print(10);
}

pfun can store the address of the function print, pfun is first combined with *, indicating that pfun1 is a pointer, the pointer points to a function, the pointed function has the parameter parameter x, and the return value type is void.

array of function pointers

To store the address of a function in an array, the array is called an array of function pointers
. For example: int (*parr[10]();
parr is first combined with [], indicating that parr is an array, and the content of the array is a function pointer of type int( )(). *

pointer to array of function pointers

A pointer to an array of functions is a pointer, a pointer to an array, and the elements of the array are function pointers

void test(const char* str){
	printf("%s\n",str);
}
int main(){
	//函数指针pfun
	void (*pfun)(const char *)=test;
	//函数指针的数组pfunArr
	void (*pfunArr[5])(const char* str);
	pfunArr[0]=test;
	//指向函数指针数组pfunArr的指针ppfunArr
	void(*(*ppfunArr)[10])(const char *)=&pfunArr;
	return 0;
}

Use of callback functions

A callback function is a function called through a function pointer. If you pass a function pointer as a parameter to another function, when the pointer is used to call the function it points to, we say this is a callback function.
Use the callback function to simulate the implementation of qsort (in the way of bubbling)

#include<stdio.h>
#include<windows.h>
#include<string.h>
int cmp(const void *p1, const void *p2){
	return (*(int *)p1 > *(int *)p2);
}
void swap(void *p1, void *p2, int size){
	for (int i = 0; i < size; i++){
		char tmp = *((char *)p1 + i);//转换为char类型想一次提取一个字节
		*((char *)p1 + i) = *((char *)p2 + i);
		*((char *)p2 + i) = tmp;
	}
}
void bubble(void *base, int count, int size, int(*cmp)(void*, void*)){
	for (int i = 0; i < count - 1; i++){
		for(int j = 0; j < count - i - 1; j++ ){
			if (cmp((char *)base + j*size, (char *)base + (j + 1)*size)>0){
			 //比较下标为j的元素的地址和下标为j+1的元素的地址
				swap((char *)base + j*size, (char *)base + (j + 1)*size, size);
			}
		}
	}
}
int main(){
	int arr[] = { 1, 3, 5, 7, 9, 2, 4, 6, 8, 0 };
	int i = 0;
	bubble(arr, sizeof(arr) / size(arr[0]), sizeof(int), cmp);
	for (i = 0; i < sizeof(arr) / sizeof(arr[0]); i++){
		printf("%d", arr[i]);
	}
	printf("\n");
	system("pause");
	return 0;
}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326627604&siteId=291194637