当前位置:网站首页>Function name pointer and function pointer
Function name pointer and function pointer
2022-07-25 17:30:00 【wei2023】
Function name pointer and function pointer
Let's first look at how function pointers are defined , If we have a function int fun(int){…}; Then the corresponding function pointer should be written as int (*p)(int); Then assign a value to him , namely p=fun; Then you can press p Calling it as a function name is completely the same as fun equally .( Notice the p Pointers are not only accepted fun This function name , Any return value is int, There is only one parameter int Can assign the function name to p)
First of all C/C++ When creating a variable, for example int a; Accordingly, one will be allocated in memory 4 Bytes ( It may be different according to different machines ) Space to store this int Variable , And suppose this 4 The starting address of bytes is 0XFF0A, In fact, there is a mapping between variable name and memory address , namely a It can be regarded as an identifier , He just represents 0XFF0A This address , In the program, you are right a In fact, the operation is to 0XFF0A First address 4 Byte operation , Especially if it's right a Perform address fetching operation, that is &a It's actually returning 0XFF0A This address value , In fact, you can see that achievement is to return a pointer to this address ( If you feel unable to understand , Just think I didn't say it ). Similarly, for the functions we create in the program , It is stored in a separate area of the program , And we need an address to uniquely point to it just like using variables , So every function needs an address to uniquely identify itself ( That is what we often call the entry address ), Just like up here a Corresponding 0XFF0A, So suppose we define a int fun(int){}; The entry address of the function is 0XAAEE, be fun That is, the function name will map 0XAAEE, And the above int Variable a If it is addressed &fun It will return 0XAAEE, actually fun It's also a type , Just think of it as a function name type , Just remember that the function name itself is not a pointer type .
It is enough to have a function name when calling a function , such as fun(2); Don't think you can call a function as long as you have a function name , In fact, this is just a confusing point in writing , The compiler will always do the so-called "Function-to-pointer conversion", That is, implicitly convert the function name to the function pointer type , That is to call the function through the function pointer , So if you call a function with (&fun)(2) It can also work , because &fun In fact, it returns a function pointer , Refer to the previous paragraph &a Example , But this kind of writing is very uncommon , Even if you don't explicitly write & The compiler will also implicitly convert , Be careful &fun The left and right parentheses must have , This is because of the priority of operators .
In fact, even if it is written (fun)(2) It can also work normally , This is because when the compiler sees fun I found there was no & That is, if he doesn't convert what is shown to him into a pointer, he will implicitly convert it into a pointer , After the conversion, I found another one in front At this time, the so-called " Quoting " operation , That is to say * Take the value from the pointer in the back , And that value actually means 0XAAEE That's the function name fun, Such an implicit transformation and then another dereference is actually equivalent to doing nothing , So the system will do another implicit "Function-to-pointer conversion", Even if you write (*******fun)(2) It will also work properly , And just a truth , It's just that I've done several more repeated conversion operations , The compiler did it by itself , Don't pay attention to !
example 1
#include<iostream>
using namespace std;
void fun(int a)
{
}
int main()
{
cout<<fun<<endl;
cout<<*fun<<endl;
cout<<&fun<<endl;
cout<<*****fun<<endl;
}
The output values are the same , That is, all pointer values to the same function address .
example 2
Now let's take a look at the function pointer defined by ourselves
#include<iostream>
using namespace std;
int fun(int a)
{
cout<<"fun"<<endl;
return 0;
}
void main()
{
int(*p)(int)=fun;
int(*p1)(int)=*fun;
int(*p2)(int)=&fun;
p(1);
p1(1);
p2(1);
}
example 3
It is found that all functions can run normally , Actually p1,p2,p and fun After the assignment, everyone can understand it equally . Follow the code
#include<iostream>
using namespace std;
int fun(int a)
{
cout<<"fun"<<endl;
return 0;
}
void main()
{
int(*p)(int)=fun;
p(1);
// (&p)(1);
(*p)(1);
(****p)(1);
}
The above programs will also run normally , As long as you understand again p Think of it as just one more conversion of the function name , Next, the understanding is the same ! Note which line commented out above cannot be run , because p Is the function pointer type defined by ourselves , If you address the pointer, you will get p The address of the variable itself , This will not call the function correctly ! One more sentence , In fact, if you run &&fun This formula is also illegal , As for why , Let's help me think , I personally think when we run &fun In fact, this pointer is only a temporary value, and the temporary value has no actual memory address, so it can't continue to get the address !
边栏推荐
- [solution] the Microsoft edge browser has the problem of "unable to access this page"
- [target detection] tph-yolov5: UAV target detection based on Transformer's improved yolov5
- Sogou batch push software - Sogou batch push tool [2022 latest]
- ACL 2022 | 基于最优传输的对比学习实现可解释的语义文本相似性
- Replicate swin on Huawei ascend910_ transformer
- MySQL数据库中去重与连接查询的方法
- 咨询下flink sql-client怎么处理DDL后,fink sql里面映射表加字段以及JOb?
- Random talk on generation diffusion model: DDPM = Bayesian + denoising
- 【解决方案】Microsoft Edge 浏览器 出现“无法访问该页面”问题
- 超越 ConvNeXt、RepLKNet | 看 51×51 卷积核如何破万卷!
猜你喜欢

Don't believe these "rumors" in the process of preparing for the exam!

Ultimate doll 2.0 | cloud native delivery package

Replicate swin on Huawei ascend910_ transformer

STM32 PAJ7620U2手势识别模块(IIC通信)程序源码详解

Outlook 教程,如何在 Outlook 中搜索日历项?

ACL 2022 | 基于最优传输的对比学习实现可解释的语义文本相似性

Using rank to discuss the solution of linear equations / the positional relationship of three planes

An article about ultrasonic humidifier

2022年最新北京建筑施工焊工(建筑特种作业)模拟题库及答案解析

Customize MVC project login registration and tree menu
随机推荐
03. Longest substring without repeated characters
Postdoctoral recruitment | West Lake University Machine Intelligence Laboratory recruitment postdoctoral / Assistant Researcher / scientific research assistant
论文阅读_多任务学习_MMoE
基于SqlSugar的开发框架循序渐进介绍(13)-- 基于ElementPlus的上传组件进行封装,便于项目使用
走马卒
电子产品“使用”和“放置”哪个寿命更长??
Is there a method in PostgreSQL that only compiles statements but does not execute them?
霸榜COCO!DINO: 让目标检测拥抱Transformer
I want to manage money. I don't understand. Is there a principal guaranteed financial product?
Technical difficulties and applications of large humanoid robots
多项式相加
实时黄金交易平台哪个可靠安全?
EDI docking commercehub orderstream
Cet
PostgreSQL passwords are case sensitive. Is there parameter control?
服务器端架构设计期末复习知识点总结
Text translation software - text batch translation converter free of charge
Tkinter module advanced operations (I) -- transparent buttons, transparent text boxes, custom buttons and custom text boxes
理财有保本产品吗?
我也是醉了,Eureka 延迟注册还有这个坑!