C language: the understanding of pointer functions and function pointers

Preface

About pointer array, the explanation of array pointer: C language: understanding of pointer array and array pointer

text

definition

// 正常函数
int fun(int x, int y);
// 指针函数
int *fun(int x, int y);
// 函数指针
int (*fun)(int x, int y);


** 指针函数本质是一个函数,其返回值为指针。**
** 函数指针本质是一个指针,其指向一个函数。**
  • Normal function : The function name fun, the two formal parameters are (int x, int y), and the return value is an intinteger.
    Statement format:类型标识符 函数名(参数)

  • Pointer function : It is a function that returns a pointer. Its essence is still a function, and the return value of the function is a pointer.
    Statement format:类型标识符 *函数名(参数)

  • Function pointer : Essentially a pointer variable , the pointer points to this function. In summary, function pointers are pointers to functions .
    Statement format:类型说明符 (*函数名)(参数)

Pointer function

concept:

Pointer function : Compared with normal function, before the function more than a *number, but the *will and intthe composition int *(int type pointer is the address) return value, and compared with normal function, but the return value is a pointer .

Writing method:
int *fun(int x, int y);
int * fun(int x, int y);
int* fun(int x, int y);

Three Kind of Writing is the same, but if you *close the return type would be better understood, the wording of the third recommendation.

Example:
#include <stdio.h>
#include <stdlib.h>

int sum = 0;
int Max(int x, int y)
{
    
    
	return (x>y?x:y); 
}

//指针函数
int* P_Add(int x, int y)
{
    
    
	sum = x + y;
	return &sum;
}

int main()
{
    
    
	int *pTemp, Temp;
	pTemp = P_Add(100,50);
	printf("Add Result: %d\n", *pTemp);
	
	Temp = Max(100,50);
	printf("Max Value: %d\n", Temp);
	return 0;
}

result

Function pointer

concept:

Function pointers : a pointer to a comparison function, to *and including the function name with a special pointer points to a function of inlet

use:

The function pointer needs to assign the address of a function to it.

int (*fun)(int x, int y);
fun = Function;

If it is a function call , use the following:

x = (*fun)();
x = fun();
Example:
#include <stdio.h>
#include <stdlib.h>

/*
* 定义一个函数指针p,只能指向返回值为int,形参为两个int 的函数
*/
int (*p)(int,int);

int Max(int x, int y)
{
    
    
	return (x>y?x:y); 
}

int Min(int x, int y)
{
    
    
	return (x<y?x:y); 
}

int main()
{
    
    
	int Num1 = 100, Num2 = 50;
	p = Max;
	printf("Max Value: %d\n", p(Num1, Num2));
	
	p = Min;
	printf("Min Value: %d\n", p(Num1, Num2));
	return 0;
}

result1

problem

After I finished writing, I was thinking about the difference between direct function name call and function pointer call , and then I found the following answer:

  • Calling directly with a function name has no advantages except for better readability. If you want to write a more general method to handle different functions, you need to use different functions to call different functions many times.
  • This problem can be avoided by using function pointers, whether it is a normal call or a formal parameter, as long as the parameter type and the return value are consistent, there is no need to reuse the function name, especially the function pointer array is widely used

Guess you like

Origin blog.csdn.net/qq_30722795/article/details/108130197