The concept of function pointers and how to define a function pointer

**Concept:** A function pointer is a pointer variable that points to a function.
Define a function pointer:
We must first know that the function type is determined by the function parameters and return value;

int Fun(int x)
{
    
    
	return x * x;
}
void test()
{
    
    
   //1,定义函数类型,通过函数类型定义函数指针
	typedef int (pFun)(int x);// 声明一个指向同样参数、返回值的函数指针类型
	pFun *p= Fun;//函数名其实就是函数的入口地址
	int a = 20;
	int ret=p(a);
	cout << "ret=" << ret << endl;

	//2,直接定义指针类型
	typedef int(*pFun2)(int x);
	pFun2 p2 = Fun;
	int ret2 = p(a);
	cout << "ret2=" << ret2 << endl;

}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324135803&siteId=291194637