当前位置:网站首页>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);
边栏推荐
- 腾讯云 TStor 统一存储通过信通院首批文件存储基础能力评测
- Using computed in uni app solves the abnormal display of data () value in tab switching
- <口算練習機 方案開發原理圖>口算練習機/口算寶/兒童數學寶/兒童計算器 LCD液晶顯示驅動IC-VK1621B,提供技術支持
- Solving the longest subsequence with linear DP -- three questions
- 字符串匹配问题
- Codeforces Round #803 (Div. 2)(A~D)
- Delete element (with transition animation)
- 数据湖(十一):Iceberg表数据组织与查询
- Fabric. JS manual bold text iText
- 博睿数据一体化智能可观测平台入选中国信通院2022年“云原生产品名录”
猜你喜欢
腾讯云 TStor 统一存储通过信通院首批文件存储基础能力评测
<口算练习机 方案开发原理图>口算练习机/口算宝/儿童数学宝/儿童计算器 LCD液晶显示驱动IC-VK1621B,提供技术支持
STM32-DAC实验&高频DAC输出测试
由粒子加速器产生的反中子形成的白洞
Penetrate the remote connection database through the Intranet
Development and design of animation surrounding mall sales website based on php+mysql
MySQL 45 lecture - learning the actual battle of MySQL in Geek time 45 Lecture Notes - 05 | easy to understand index (Part 2)
How kaggle uses utility script
什么是 eRDMA?丨科普漫画图解
Origin plots thermogravimetric TG and differential thermogravimetric DTG curves
随机推荐
Advanced usage of C language -- function pointer: callback function; Conversion table
Delete element (with transition animation)
Fabric. JS upper dash, middle dash (strikethrough), underline
Analysis of CPU surge in production environment service
Method of creating linked server for cross server data access
Adhere to the foundation of 20 minutes go every day II
Fabric. JS manual bold text iText
默认插槽,具名插槽,作用域插槽
kaggle如何使用utility script
Golang quickly generates model and queryset of database tables
Development and design of animation surrounding mall sales website based on php+mysql
Generally speaking, if the error of inconsistent tab and space occurs frequently
每日学习3
<口算練習機 方案開發原理圖>口算練習機/口算寶/兒童數學寶/兒童計算器 LCD液晶顯示驅動IC-VK1621B,提供技術支持
《可供方案开发》口算训练机/数学宝/儿童口算宝/智能数学宝 LCD液晶显示驱动IC-VK1622(LQFP64封装),原厂技术支持
Golang 快速生成数据库表的 model 和 queryset
Launcher启动过程
Convolutional neural network (Introduction)
[Hongke technology sharing] how to test DNS server: DNS performance and response time test
Who is better, Qianyuan projection Xiaoming Q1 pro or Jimi new play? Which configuration is higher than haqu K1?