当前位置:网站首页>3. Function pointers and pointer functions
3. Function pointers and pointer functions
2022-07-02 14:31:00 【Little BigUs】
Function pointers and pointer functions
A function pointer
A function pointer is a pointer , Point to a function , The return value and parameter list of the pointed function must be the same as The return value of pointer definition is the same as the parameter list .
For example, the following code :
#include<stdio.h>
void pp()
{
printf("this is function pp!\n");
}
void ppp()
{
printf("this is function ppp!\n");
}
int main(void)
{
void (*p)();
p = pp;
p();
p = ppp;
p();
return 0;
}
It defines a point The return value is void, Function pointer with empty argument list , And two functions are defined pp and ppp, And then they will p Point to these two functions and call the function . This is the function pointer .*p The surrounding brackets must not be omitted , Otherwise it will be wrong . Use () after p With the first * combination , therefore p It's a pointer .
Note that when assigning values above, the function name is directly assigned to the pointer , We all know that the pointer points to the address , The previous pointer to the variable needs to add a & This address symbol , When the function pointer points to a function, you don't need , This shows that the function name is actually an address ,
printf("%p\n",pp);
printf("%p\n",&pp);
We can try printing the above two values again , One is printing pp Value , One is printing pp The address of , We can find that these two values are the same , That is to say whether pp still &pp All represent addresses , So the assignment of the previous pointer can not only be p = pp; It can also be p = &pp; Of course, we usually use p = pp; It's not convenient to lose one symbol ?
Pointer function
The pointer function is a function , The return value of this function is a pointer .
for example int* f(int a, int b); Is the definition of a pointer function , The return value of this function must be a int Pointer to type .
() Has a higher priority than *, therefore f With the first () combination ,f It's a function .
Function pointer array
seeing the name of a thing one thinks of its function , This is an array , Every element in the array is a function pointer , A function pointer is a pointer to a function . All elements in an array must be of the same type , So all function pointers in the array must point to functions of the same type .
int (*p[10])(int a, int b);
边栏推荐
- Federated Search: all requirements in search
- MySQL 45 lecture - learning from the actual battle of geek time MySQL 45 Lecture Notes - 04 | easy to understand index (Part 1)
- Default slot, named slot, scope slot
- Fabric.js 缩放画布
- In 2021, the global revenue of structural bolts was about $796.4 million, and it is expected to reach $1097.6 million in 2028
- Codeforces Round #803 (Div. 2)(A~D)
- Design of non main lamp: how to make intelligent lighting more "intelligent"?
- Fabric. JS free draw circle
- Custom events, global event bus, message subscription and publishing, $nexttick
- Do you know that there is an upper limit on the size of Oracle data files?
猜你喜欢

Fabric. Usage of JS eraser (including recovery function)

Fabric.js 橡皮擦的用法(包含恢复功能)

C crystal report printing
![[development environment] Dell computer system reinstallation (download Dell OS recovery tool | use Dell OS recovery tool to make USB flash disk system | install system)](/img/e0/e9cb42f241a60995d4598ba6a1a2fe.jpg)
[development environment] Dell computer system reinstallation (download Dell OS recovery tool | use Dell OS recovery tool to make USB flash disk system | install system)

Chinese science and technology from the Winter Olympics (III): the awakening and evolution of digital people
![[deep learning] simple implementation of neural network forward propagation](/img/a6/9b4896c62e9b77f9d528c3c9efc58f.jpg)
[deep learning] simple implementation of neural network forward propagation

< schematic diagram of oral arithmetic exercise machine program development> oral arithmetic exercise machine / oral arithmetic treasure / children's math treasure / children's calculator LCD LCD driv

Custom events, global event bus, message subscription and publishing, $nexttick

In 2021, the global revenue of structural bolts was about $796.4 million, and it is expected to reach $1097.6 million in 2028

Codeforces Round #803 (Div. 2)(A~D)
随机推荐
由粒子加速器产生的反中子形成的白洞
Design and implementation of car query system based on php+mysql
String matching problem
测试框架TestNG的使用(二):testNG xml的使用
Slashgear shares 2021 life changing technology products, which are somewhat unexpected
How kaggle uses utility script
提示:SQL Server 阻止了对组件‘Ad Hoc Distributed Queries ‘的STATEMENT ‘OpenRowset/OpenDatasource“”
P1908 reverse sequence pair
Borui data integrated intelligent observable platform was selected into the "Yunyuan production catalogue" of China Academy of communications in 2022
The use of TestNG, the testing framework (II): the use of TestNG XML
卷积神经网络(入门)
Pycharm连接远程服务器
P1908 逆序对
Fabric. JS free draw circle
mongodb的认识
Launcher startup process
每日学习2
PyQt5_QScrollArea内容保存成图片
途家木鸟美团夏日折扣对垒,门槛低就一定香吗?
线性dp求解 最长子序列 —— 小题三则