[C language] pointer function pointer

Article Directory

One, the concept of function pointer

Second, the application of function pointers

(1) Call function

(2) Pass the address of the function as a function parameter to other functions.

Three, example demonstration


 

One, the concept of function pointer

Definition of function pointer

If a function is defined in the program, when compiling, the compiler will allocate a section of storage space for the function code. The starting address (also known as the entry address) of this space is called the pointer of this function.

As with ordinary variables, a pointer can be defined to point to the starting address of the storage space where the function code is stored. Such a pointer is a function pointer .

The definition format of the function pointer is as follows:

Return value type (*variable name) (parameter list)

The return value type is the return value type of the pointer to the function, * represents a pointer variable, and the parameter list is the formal parameter list of the pointer to the function.

[Note] Because * has a higher priority, you should enclose * and the variable name

Examples:

int func(int a,int b);//function declaration

int (*p) (int, int );//Define a parameter list as two variables of type int, and the return value type is a function pointer p of type int

p=func;//The function pointer p points to the starting address of the function func 


Second, the application of function pointers

(1) Call function

Use the function pointer to call the corresponding function. The method is similar to that of using the function name to call the function, just replace the function name with (*pointer name)

Call the function pointed to by the function pointer p

(*p)(3,5);

(2) Pass the address of the function as a function parameter to other functions.

By passing the address of the function into other parameters, the actual parameter function can be used in the called function.

Pass the address of func as a parameter to the func2 function

void func2(int(*p)(int,int),int b,int c);


Three, example demonstration

#include <stdio.h>

void sum_row(int(*arr)[4], int raw, int* sum);//行求和函数声明
void sum_col(int(*arr)[4], int raw, int* sum);//列求和函数声明

//行求和函数定义
void sum_row(int(*arr)[4], int raw, int* sum) {
	int i = 0;
	*sum = 0;
	for (; i < 4; i++) {
		*sum += (*(*(arr + raw-1) + i));
	}
}

//列求和函数定义
void sum_col(int(*arr)[4], int col, int* sum) {
	int i = 0;
	*sum = 0;
	for (; i < 3; i++) {
		*sum += (*(*(arr +i ) + (col-1)));
	}
	
}

int main() {
	int matrix[3][4];
	int i, j;
	
	for (i = 0; i < 3; i++) {
		for (j = 0; j < 4; j++) {
			matrix[i][j] = i * 4 + j;
		}
	}
	printf("输出初始矩阵:\n");
	int(*p)[4];//定义数组指针p
	p = matrix;
	for (i = 0; i < 3; i++) {
		for (j = 0; j < 4; j++) {
			printf("%-5d", *(*(p + i) + j));
		}
		printf("\n");
	}

	int sum;
	void (*q)(int(*arr)[4], int raw, int* sum);//定义函数指针
	printf("计算第一行的和:\n");
	q = sum_row;
	(*q)(matrix, 1, &sum);//调用行求和函数
	printf("sum=%d\n",sum);

	printf("计算第一列的和:\n");
	q = sum_col;
	(*q)(matrix, 1, &sum);//调用列求和函数
	printf("sum=%d\n", sum);
}

operation result:

 

Guess you like

Origin blog.csdn.net/Jacky_Feng/article/details/108953519