当前位置:网站首页>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 !
边栏推荐
- 电子产品EMC不合格,如何整改?
- The gas is exhausted! After 23 years of operation, the former "largest e-commerce website in China" has become yellow...
- window10系统下nvm的安装步骤以及使用方法
- 世界各地的标志性建筑物
- 更新|3DCAT实时云渲染 v2.1.2版本全新发布
- Google Earth engine - download the globalmlbuildingfootprints vector collection of global buildings
- With 8 years of product experience, I have summarized these practical experience of continuous and efficient research and development
- Crawler framework crawler
- 做智能硬件要考虑的产品生命周期
- PostgreSQL里有只编译语句但不执行的方法吗?
猜你喜欢

I'm also drunk. Eureka delayed registration and this pit!

I2C communication - sequence diagram

stm32F407------SPI

EDI 对接CommerceHub OrderStream
![[solution] the Microsoft edge browser has the problem of](/img/47/7e20a4f1e04577153e7cf0a6c61f26.png)
[solution] the Microsoft edge browser has the problem of "unable to access this page"

POWERBOARD coco! Dino: let target detection embrace transformer

哈夫曼树的构建

How to fix the first row title when scrolling down in Excel table / WPS table?

Hcip notes 11 days

Using rank to discuss the solution of linear equations / the positional relationship of three planes
随机推荐
【Cadence Allegro PCB设计】永久修改快捷键(自定义)~亲测有效~
走马卒
postgreSQL 密码区分大小写 ,有参数控制吗?
What financial products can you buy to make money with only 1000 yuan?
函数名指针和函数指针
Replicate swin on Huawei ascend910_ transformer
电子产品EMC不合格,如何整改?
一篇文章了解超声波加湿器
01. Sum of two numbers
【硬件工程师】关于信号电平驱动能力
After consulting about how to deal with DDL in Flink SQL client, how to add fields and jobs to the mapping table in Fink SQL?
双向链表的基本操作
Boring post roast about work and life
HCIP笔记十一天
04.寻找两个正序数组的中位数
Interface automation test postman+newman+jenkins
Enumeration classes and magic values
我想理财,不懂,有没有保本金的理财产品?
大型仿人机器人的技术难点和应用情况
Chapter III data types and variables