当前位置:网站首页>3、函数指针和指针函数
3、函数指针和指针函数
2022-07-02 11:21:00 【Little BigUs】
函数指针和指针函数
函数指针
函数指针是一个指针,指向一个函数,这个被指向的函数的返回值和参数列表必须和 指针定义时的返回值和参数列表相同。
例如下面的代码:
#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;
}
就定义了一个指向 返回值为void,参数列表为空的函数指针,并且定义了两个函数pp和ppp,然后分别将p指向这两个函数并且调用函数。这就是函数指针。*p周围的括号一定不能省略,否则会错。使用()之后p先与*结合,所以p是一个指针。
注意在上面赋值的时候是直接把函数名赋值给指针,我们都知道指针指向的是地址,而前面指向变量的指针需要在变量前面加一个&这个取地址的符号,而函数指针指向函数的时候不需要,这说明其实函数名就是一个地址,
printf("%p\n",pp);
printf("%p\n",&pp);
我们可以再试试打印上面的两个值,一个是打印pp的值,一个是打印pp的地址,我们可以发现这两个值都是一样的,也就是说无论是pp还是&pp都是代表地址,所以前面指针的赋值不仅可以是p = pp;也可以是p = &pp;当然我们一般都是使用p = pp;少一个符号岂不方便?
指针函数
而指针函数则是一个函数,这个函数的返回值是一个指针。
例如int* f(int a, int b);就是一个指针函数的定义,这个函数的返回值必须是一个int型的指针。
()的优先级高于*,所以f先与()结合,f是一个函数。
函数指针数组
顾名思义,这是一个数组,数组中的每一个元素都是一个函数指针,而函数指针是一个指向函数的指针。数组中所有元素的类型必须相同,所以数组中所有的函数指针都必须指向同类型的函数。
int (*p[10])(int a, int b);
边栏推荐
- In 2021, the global styrene butadiene styrene (SBS) revenue was about $3722.7 million, and it is expected to reach $5679.6 million in 2028
- Daily learning 3
- Codeforces Round #803 (Div. 2)(A~D)
- 途家木鸟美团夏日折扣对垒,门槛低就一定香吗?
- Systemserver process
- Who is better, Qianyuan projection Xiaoming Q1 pro or Jimi new play? Which configuration is higher than haqu K1?
- 当贝投影4K激光投影X3 Pro获得一致好评:万元投影仪首选
- 提示:SQL Server 阻止了对组件‘Ad Hoc Distributed Queries ‘的STATEMENT ‘OpenRowset/OpenDatasource“”
- 【虹科技术分享】如何测试 DNS 服务器:DNS 性能和响应时间测试
- Penetrate the remote connection database through the Intranet
猜你喜欢

In 2021, the global TCB adapter revenue was about $93 million, and it is expected to reach $315.5 million in 2028

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 '
![[to be continued] [UE4 notes] l5ue4 model import](/img/6b/d3083afc969043dbef1aeb4fccfc99.jpg)
[to be continued] [UE4 notes] l5ue4 model import

Available solution development oral arithmetic training machine / math treasure / children's oral arithmetic treasure / intelligent math treasure LCD LCD driver ic-vk1622 (lqfp64 package), original te

你知道Oracle的数据文件大小有上限么?

途家木鸟美团夏日折扣对垒,门槛低就一定香吗?

跨服务器数据访问的创建链接服务器方法

Use of UIC in QT

php链表创建和遍历

无主灯设计:如何让智能照明更加「智能」?
随机推荐
Understanding of mongodb
How to set QT manual layout
跨服务器数据访问的创建链接服务器方法
测试框架TestNG的使用(二):testNG xml的使用
每日学习3
Who is better, Qianyuan projection Xiaoming Q1 pro or Jimi new play? Which configuration is higher than haqu K1?
< 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
快解析:轻松实现共享上网
Fabric.js 缩放画布
<口算练习机 方案开发原理图>口算练习机/口算宝/儿童数学宝/儿童计算器 LCD液晶显示驱动IC-VK1621B,提供技术支持
NLA自然语言分析实现数据分析零门槛
Analysis of CPU surge in production environment service
<口算練習機 方案開發原理圖>口算練習機/口算寶/兒童數學寶/兒童計算器 LCD液晶顯示驅動IC-VK1621B,提供技術支持
Getting started with QT - making a simple calculator
[to be continued] [UE4 notes] l5ue4 model import
What is erdma? Popular science cartoon illustration
c# 水晶报表打印
How kaggle uses utility script
Systemserver process
PyQt5_QScrollArea内容保存成图片