当前位置:网站首页>Advanced C language -- function pointer
Advanced C language -- function pointer
2022-07-07 16:35:00 【chencli】
Let's look at a code first :
#include<stdio.h>
void test()
{
printf("haha\n");
}
int main()
{
printf("%p\n", test);
printf("%p\n", &test);
return 0;
}
The output is two addresses , The name of the function is the address of the function
Save the address of the function :
#include<stdio.h>
void test()
{
printf("haha\n");
}
int main()
{
// A function pointer
int (*pf)(const char*) = test;
(*pf)("abc");
pf("abc");
test("abc");
return 0;
}
Function pointers are also pointers , Is a pointer to a function
int (*pf)(const char*) = test
pf The first and * combination , Is a pointer , Point to test function , No parameter , The return value type is void
《C Pitfalls and pitfalls 》 A piece of code :
( *(void (*)())0 )();
void(*)() It's a function pointer type
( void (*)() )0 Is a cast , The result is the address of the function ,0 Store a function in the address , No parameter , No return value
The above code is generally a function call , It's called 0 As a function at the address , First turn on the 0 Cast to no parameter , The return type is void The address of the function , The second is to call 0 This function at the address
Look at this code again :
void (*signal(int , void(*)(int)))(int);
Code can be simplified :
hold void(*)(int) Rename it to pfun_t
typedef void(*pfun_t)(int);
pfun_t signal(int, pfun_t);
signal Combine with the following parentheses , Is the function name
( int , void(*)(int) ) Are two parameter types
The above code is a function declaration ,signal The type of the first argument to the function is int, The second parameter type is the function pointer , The pointer points to a parameter of type int, Function with null return value ,signal The return type of a function is also a pointer function , The function pointer also points to a parameter of type int, Function with null return value
Use function pointers to simplify code :
When there are more identical codes in functions with similar functions , Function pointers can be used to simplify code
void calc( int(*pf) (int, int) )
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
void menu()
{
printf("*****************\n");
printf("***1.sum 2.sub***\n");
printf("***3.mul 4.div***\n");
printf("*****0. sign out *****\n");
printf("*********\n");
}
int add(int x, int y)
{
return x + y;
}
int sub(int x, int y)
{
return x - y;
}
int mul(int x, int y)
{
return x * y;
}
int div(int x, int y)
{
return x / y;
}
// Calculation
void calc(int(*pf)(int, int))
{
int x = 0;
int y = 0;
int ret = 0;
printf(" Enter two operands :");
scanf("%d%d", &x, &y);
ret = pf(x, y);
printf("%d\n", ret);
}
int main()
{
int input = 0;
do
{
menu();
printf(" Please select ");
scanf("%d", &input);
switch(input)
{
case 1:
calc(add);
break;
case 2:
calc(sub);
break;
case 3:
calc(mul);
break;
case 4:
calc(div);
break;
case 0:
printf(" Exit calculator !\n");
break;
default:
printf(" Wrong choice !\n");
break;
}
} while (input);
return 0;
}
The above code uses the callback function , Callback function It's a pass through A function pointer Called function . If you put Pointer to function ( Address ) Pass as argument to another function , When this pointer is used to call the function it points to , Let's just say this is a callback function . The callback function is not called directly by the function's implementer , It's called by another party when a particular event or condition occurs , Used to respond to the event or condition .
边栏推荐
- Plate - forme de surveillance par étapes zabbix
- Odoo integrated plausible embedded code monitoring platform
- hellogolang
- Lecturer solicitation order | Apache seatunnel (cultivating) meetup sharing guests are in hot Recruitment!
- Opencv configuration 2019vs
- Horizontal and vertical centering method and compatibility
- 记一次项目的迁移过程
- Continuous creation depends on it!
- 47_Opencv中的轮廓查找 cv::findContours()
- How can laravel get the public path
猜你喜欢
Cesium(3):ThirdParty/zip. js
二叉搜索树(基操篇)
Mysql database basic operation DQL basic query
The team of East China Normal University proposed the systematic molecular implementation of convolutional neural network with DNA regulation circuit
Have fun | latest progress of "spacecraft program" activities
Rongyun won the 2022 China Xinchuang digital office portal excellence product award!
AutoLISP series (2): function function 2
[flower carving experience] 15 try to build the Arduino development environment of beetle esp32 C3
Plate - forme de surveillance par étapes zabbix
无法将“pip”项识别为 cmdlet、函数、脚本文件或可运行程序的名称
随机推荐
php 自带过滤和转义函数
偶然升职的内心独白
Opportunity interview experience summary
Laravel 中config的用法
记录Servlet学习时的一次乱码
平衡二叉树(AVL)
谈谈 SAP iRPA Studio 创建的本地项目的云端部署问题
Prediction - Grey Prediction
Geoserver2.18 series (5): connect to SQLSERVER database
MySQL中, 如何查询某一天, 某一月, 某一年的数据
Xcode Revoke certificate
修改配置文件后tidb无法启动
Enterprise log analysis system elk
模仿企业微信会议室选择
laravel post提交数据时显示异常
作为Android开发程序员,android高级面试
【HCSD大咖直播】亲授大厂面试秘诀-简要笔记
47_Opencv中的轮廓查找 cv::findContours()
【Vulnhub靶场】THALES:1
深度监听 数组深度监听 watch