当前位置:网站首页>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);
边栏推荐
- <口算練習機 方案開發原理圖>口算練習機/口算寶/兒童數學寶/兒童計算器 LCD液晶顯示驅動IC-VK1621B,提供技術支持
- 每日学习2
- MQ tutorial | exchange (switch)
- 篇9:XShell免费版安装
- Use of swagger
- docker mysql
- freemarker的使用
- Using computed in uni app solves the abnormal display of data () value in tab switching
- PHP linked list creation and traversal
- Codeforces Round #803 (Div. 2)(A~D)
猜你喜欢
<口算练习机 方案开发原理图>口算练习机/口算宝/儿童数学宝/儿童计算器 LCD液晶显示驱动IC-VK1621B,提供技术支持
Design and implementation of car query system based on php+mysql
Design of non main lamp: how to make intelligent lighting more "intelligent"?
A white hole formed by antineutrons produced by particle accelerators
How kaggle uses utility script
你知道Oracle的数据文件大小有上限么?
腾讯云 TStor 统一存储通过信通院首批文件存储基础能力评测
There is no solution to the decryption error of the remote user 'sa' and the service master password mapped from the remote server 'to the local user' (null) /sa '
PHP linked list creation and traversal
Systemserver process
随机推荐
kaggle如何使用utility script
The use of TestNG, the testing framework (II): the use of TestNG XML
数据湖(十一):Iceberg表数据组织与查询
Fabric.js 自由绘制圆形
腾讯云 TStor 统一存储通过信通院首批文件存储基础能力评测
Default slot, named slot, scope slot
Methods of software testing
Design and implementation of car query system based on php+mysql
默认插槽,具名插槽,作用域插槽
Analysis of CPU surge in production environment service
There is no solution to the decryption error of the remote user 'sa' and the service master password mapped from the remote server 'to the local user' (null) /sa '
线性dp求解 最长子序列 —— 小题三则
The most complete analysis of Flink frame window function
Fabric.js 自由绘制椭圆
NLA自然语言分析实现数据分析零门槛
Tujia muniao meituan has a discount match in summer. Will it be fragrant if the threshold is low?
P1042 [noip2003 popularization group] Table Tennis
go操作redis
freemarker的使用
The evolution process of the correct implementation principle of redis distributed lock and the summary of redison's actual combat