当前位置:网站首页>[advanced C language] ⑥ detailed explanation of function pointer
[advanced C language] ⑥ detailed explanation of function pointer
2022-04-21 19:05:00 【Poplar CC】
One 、 A function pointer
1. Concept
A function pointer : First of all, it's a pointer , A pointer to a function , The address of the function is stored in the memory space ;
See the example :
int main(){
int a = 10;
int*pa = &a;
char ch = 'c';
char* pc = &ch;
int arr[10] = {
0};
int (*parr)[10] = &arr;// Take the address of the array
return 0;
}
analysis :parr Is a pointer to an array , It stores the address of the array ;
therefore :
- Array pointer — Pointer to the address of the array ;
- & Array name — What you get is the address of the array ;
So can we think so :
- A function pointer — A pointer to the address of the function ;
- & Function name — What you get is the address of a function ;
Is that right ? So let's test that out , Look at the following examples :
int Add(int x,int y)
{
return x+y;
}
int main()
{
printf("%p\n",&Add);// Print the function Add() The address of
printf("%p\n",Add);// The array name is equal to the address of the first element of the array , Is the function name equal to the function address ?
return 0;
}
Please see the result :
Oh ! original , The function name is equal to the function address Of !
1.2 How to use function pointers
Definition of function pointer : Return value type of function (* Pointer name )( The argument list type of the function )
int Add(int x, int y)
{
return x+y;
}
int main()
{
int (*pf)(int, int) = &Add;// Function pointer definition , The return value type and parameter type are the same as the function Add() identical
}
1.3 Practice consolidating
void test(char* str){
}
int main (){
①(?)pt =&test;
return 0;
}
Excuse me, ① How should the sentence be perfect ?
answer :void ( * pt)(char*) = &test;
How to use function pointers to call functions ?
Or the example above :
void Add(int x, int y){
return x+y;}
int main(){
int (*pf)(int,int)=&Add;
int ret=(*pf)(3,5);
analysis :
int ret=(*pf)(3,5), At this point, it is equivalent to calling... Through the function name : int ret=Add(3,5);}, We know : The function name is equal to & Function name Of , therefore int (*pf)(int,int)=&Add, It can be changed to :int (*pf)(int,int)=Add; here Add Equivalent to pf, therefore :int ret=(*pf)(3,5); The statement can be changed to :int ret=pf(3,5); Equivalent to int ret=Add(3,5), So we know that for :int ret=(*pf)(3,5); The statement is ,* It doesn't make sense , Having one or more or none does not affect ;
1.4 To sum up
- Array name (arr) != & Array name (&arr)
- Function name (Add) = & Function name (&Add)
Two 、 Read two interesting pieces of code
notes : originate 《c Pitfalls and pitfalls 》;
1.( *(void( *)( ))0 )( )
analysis :
The meaning of this code is :
- call 0 The function at the address
- This function has no parameters , The return value is void
- Split :
●void()() Indicates the function pointer type
●( void()() )0 Said to 0 Cast , hold 0 Cast the type to the address of a function ; Such as (int)3.14
●* ( void()() )0 Said to 0 The function at the address to understand the reference operation
●( ( void(*)() )0)() It means calling 0 The function at the address- Look at the diagram :
2.void (* signal(int,void( * )( int ) ) )(int)
analysis :
- signal and () First combine , explain signal Is a function name
- signal The type of the first parameter of the function is int, The second parameter is of type function pointer , The function pointer points to a parameter int, The return value is void Function of ;
- signal The return type of a function is also a function pointer , The function pointer , Point to a parameter that is int, The return value is void function
4. Look at the diagram :
Sum up ,signal It's a function declaration ;
版权声明
本文为[Poplar CC]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204211900475655.html
边栏推荐
- SIGIR'22 "Ali" metacvr: meta learning alleviates the problem of data distribution fluctuation in small-scale recommendation
- 替代JC-1,MITO-ID系列线粒体膜电位检测试剂盒方案
- APM 行业认知系列 - 四
- 《Tensorflow 从基础到实战》00 张量,会话,变量,矩阵
- [talkative cloud native] load balancing - the passenger flow of small restaurants has increased
- shardingJdbc的LocalDateTime问题
- MySQL去重查询
- emement中的el-input 搜索时输入后匹配输入建议必须有value属性
- 使用MCUXpresso开发RT1060(1)——驱动RGB接口LCD
- 2021年拍卖行业发展研究报告
猜你喜欢
![[deep eye] emotion analysis -- Text Classification textrnn based on cyclic neural network for multi task learning](/img/5b/e34bcd4d5b08ab6cd01aaf1e4ebe2a.png)
[deep eye] emotion analysis -- Text Classification textrnn based on cyclic neural network for multi task learning

Crystal Chem小鼠葡萄糖检测试剂盒说明书

LeetCode1765. Highest point in the map (BFS)

Excel表格快速生成LaTeX

U-Net网络变形综述

Wide application of medical robot in AI field

Rk3399—添加usb转串口驱动

DVWA-Brute Force

An important trend in the development of children's programming training

Chinese NER Using Lattice LSTM 论文解读
随机推荐
EasyGBS关闭了录像计划,为何还有录像文件生成?
APM industry awareness series - IX
改进模型速度/精度的工程方法
Apply El tooltip (bubble text prompt box) in El tabs
"Actual combat" realizes linear regression with tensorflow
2022.04.21(LC_56_合并区间)
医疗机器人在AI领域的广泛应用
Nacos之服务注册源码分析
[untitled]
Tensorflow from foundation to practice 00 tensor, conversation, variable, matrix
AlwaysInstallElevated提权
SIGIR'22 "Ali" metacvr: meta learning alleviates the problem of data distribution fluctuation in small-scale recommendation
el-tabs中套用el-tooltip(气泡文字提示框)
刚毕业大学生考PMP用处大吗?
排序会了递归,不学非递归太可惜了
EdgeBoard记录
mysql不能使用 mysql -u root -p 启动报错解决
【C语言进阶】⑥函数指针详解
Clickhouse installation configuration
Crystal Chem小鼠葡萄糖检测试剂盒说明书


