当前位置:网站首页>Function pointers and pointer functions
Function pointers and pointer functions
2022-04-22 08:40:00 【weixin_ forty-six million two hundred and seventy-two thousand 】
List of articles
Pointer function
Functions that return pointers
int* array_() {
int* ptr = (int*)malloc(10 * sizeof(int));
for (int i = 0; i < 10; i++) {
*(ptr + i) = 10;
}
return ptr;
}
int main() {
int* temp = array_();
for (int i = 0; i < 10; i++) {
cout << *(temp + i) << " ";
}
cout << endl;
return 0;
}
10 10 10 10 10 10 10 10 10 10
A function pointer
Declared function pointer
// Declare a function pointer
void (*foo) ();

As shown in the figure , Declaration of function pointer
Be careful , Don't mix up. A function pointer and Pointer function
int (*fptrl)(int, int); // Pointer to function
int* function(int, int); // A function that returns a pointer type
We can also use typedef Declare a type definition
typedef int (*funcptr)(int,int);//funcptr For this type
funcptr fptr2 = sum;
Calling a function through a function pointer
The function pointer stores the address of the function , Used to point to a function , Different functions can be called by function pointers
But the function pointer must be of the same type as the function it points to , in other words , The return value type and parameter type should be consistent with the number of parameters
// Declared function pointer
typedef int (*function_ptr)(int, int);
// The function that needs to be pointed to
int sum(int a, int b) {
return a + b;
}
int main()
{
//fptrl = sum2; Type mismatch , This returns to double type , I also accept two double Parameters of
function_ptr fptrl = sum;
cout << fptrl(2, 3) << endl; // 5
return 0;
}
In this case , We use a function pointer to call a function , But we didn't add... Before the function &, The function name is similar to the array name , You can directly return the address of the function
int (*fptrl) (int);
int square(int num) {
return num * num;
}
int main()
{
fptrl = square;
cout << fptrl(2) << endl; //4
return 0;
}

This is an example of calling another function , Understand how functions in memory are called
Pass function pointer
Just declare the function pointer as a function parameter
// Declared function pointer
typedef int (*fptrOperation)(int, int);
// Add
int add(int num1, int num2) {
return num1 + num2;
}
// Subtraction
int sub(int num1, int num2) {
return num1 - num2;
}
// Accept function pointer , And two for calculation int Parameters
int compute(fptrOperation operation, int a, int b) {
return operation(a, b);// Returns the result of the operation
}
int main()
{
cout << compute(add, 5, 6);
cout << compute(sub, 5, 6);
/*fptrOperation fptr = sum; cout << "the result(+) is: " << compute(add, 10, 5) << endl; cout << "the result(+) is: " << compute(fptr, 10, 5) << endl; cout << "the result(+) is: " << evaluate('+', 10, 5) << endl; fptr = sub; cout << "the result(-) is: " << compute(sub, 10, 5) << endl; cout << "the result(-) is: " << compute(fptr, 10, 5) << endl; cout << "the result(+) is: " << evaluate('-', 10, 5) << endl;*/
return 0;
}
Return function pointer
typedef int (*fptrOperation)(int, int);
// Add
int add(int num1, int num2) {
return num1 + num2;
}
// Subtraction
int sub(int num1, int num2) {
return num1 - num2;
}
// Calculation
int compute(fptrOperation operation, int a, int b) {
return operation(a, b);
}
// Returns the function pointer according to the character
fptrOperation select(char opcode) {
switch (opcode) {
case '+': return add;
case '-': return sub;
}
}
// Accept characters and call select function , Accept select Function pointer returned , Call the function pointed to by the function pointer
int evaluate(char opcode, int num1, int num2) {
fptrOperation operation = select(opcode);
return operation(num1, num2);
}
int main()
{
fptrOperation fptr = add;
cout << "the result(+) is: " << compute(add, 10, 5) << endl;
cout << "the result(+) is: " << compute(fptr, 10, 5) << endl;
cout << "the result(+) is: " << evaluate('+', 10, 5) << endl;
fptr = sub;
cout << "the result(-) is: " << compute(sub, 10, 5) << endl;
cout << "the result(-) is: " << compute(fptr, 10, 5) << endl;
cout << "the result(+) is: " << evaluate('-', 10, 5) << endl;
return 0;
}
See here , Did you learn C++ Do you feel a little familiar , That's the smell of polymorphism ~
So , It's important to lay a good foundation !
版权声明
本文为[weixin_ forty-six million two hundred and seventy-two thousand ]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220742291314.html
边栏推荐
猜你喜欢
随机推荐
Viewpager comprehensive summary
The map cutter can cut the picture into tile data. Its main purpose is to cut the high-definition satellite image into tile map. It can be used for offline map development based on Mercator coordinate
centos7安装MySQL8.0
Binary leading zero
shell监控IBM MQ队列深度,10s扫描三次,有两次以上深度值超过5时,则输出队列名称和深度值。
客户端与服务器项目3
Introduction to smarttablayout
SQL database multiple choice question (2)
第2关:ACL访问控制列表
[跟着官方文档学JUnit5][二][WritingTests][学习笔记]
CentOS 安裝 MySQL
构造函数与toString
Redefine China's "core"
微信公众号——网页授权
cesium鼠标拾取要素,并判断要素类别
shell学习笔记——shell对输出流的处理awk
leaflet、cesium加载百度地图,加载自定义样式百度地图
华为机试题——HJ53 杨辉三角的变形
111. 二叉树的最小深度
Shell command script









