当前位置:网站首页>C语言的指针函数
C语言的指针函数
2022-07-17 05:08:00 【学会放下ta】
可变参数函数
可变参数函数需要特定头文件,有特定的格式,请看下例:
#include <stdio.h>
#include <stdarg.h>//首先引用特定头文件
//写一个求和函数
int sum(int n,...);//声明&定义 n表示参数个数 ...表示参数不定
int sum(int n,...)
{
int i,sum=0;
va_list vap;//加载参数列表
va_start(vap,n);//打开参数列表
for(i=0;i<n;i++)
{
sum+=va_arg(vap,int);//使用参数
}
va_end(vap);//关闭参数列表
return sum;//返回值
}
指针函数
指针函数就是返回一个指针的函数
举个例子:
char* xxx(int a)
{
if(a==1)
return "just do it!";//返回的是这个字符串第一个字符的地址
...
}
注意:不能返回局部变量的指针
函数指针
是一个指向函数的指针,跟指针数组与数组指针关系差不多
int square(int a)
{
return a*a;
}
int (* xxx)(int x);//定义一个函数指针xxx指向一个有一个int参数的函数
xxx = □//给xxx初始化
(*xxx)(2);//通过函数指针调用函数,也可以写成xxx(2),但是为了与函数名区别,加括号和*
函数指针作为函数参数
int add(int num1,int num2)
{
return (num1+num2);
}
int a(int (*b)(int , int),int num1,int num2)//定义一个函数,三个参数(看逗号),一个参数是函数指针,指向的函数返回值是int,有两个int参数,另外两个参数是int
{
return (*b)(num1,num2);
}
a(add,1,3);//然后直接调用
将函数指针作为返回值
int (*select(char))(int num1,int num2);
根据C语言从左到右从内到外的顺序解析一下这个定义:select(char)首先确认了这是一个名为select的函数,有一个char参数,剩下的就是int (*)(int num1,int num2)返回值:一个函数指针,指向有两个int参数的函数
边栏推荐
- 2020-11-10
- Redis source code analysis - data structure and Implementation (Dictionary dict)
- Wechat applet learning notes
- Opendds QoS and custom QoS (timing, timingqospolicy)
- Cesium 綁定鼠標事件和移除鼠標事件
- redis 源码分析 动态字符串实现(sds)
- [ES6] quickly print user information to the page
- H5 how to obtain intranet IP and public IP
- 软件测试就业前景怎样 人才需求大,岗位稳定性强
- 运维安全要了解的二三事
猜你喜欢

Talk about 20 negative teaching materials for writing code

Interviewer: how to solve the problem of a large number of requests for data that does not exist in redis, which affects the database?

操作系统常见面试题

Network command: network card information, netstat, ARP

软件测试就业前景怎样 人才需求大,岗位稳定性强

Performance bottleneck finding - Flame graph analysis

Teach you to reproduce log4j2 nuclear weapon level vulnerability hand in hand

Redis 源码分析-数据结构及实现(字典dict)

【全网首发】一个月后,我们又从 MySQL 双主切换成了主-从

Wechat applet learning notes
随机推荐
UML(用例图,类图,对象图,包图)
mysql的锁
微信小程序 学习笔记
Easypoi excel simple export
web3js开发技术
Markdown notes and related shortcut keys of typora
Web3js development technology
线程池如何监控,才能帮助开发者快速定位线上错误?
[ES6] use multiple functions such as adding data, filtering and transmitting to the page to realize cases
Excel导入长数据末尾变000
Mapbox loads local offline terrain
Cesium geojson数据的添加与移除
Cesium bind mouse events and remove mouse events
Cesium BIND Mouse Events and remove Mouse Events
Performance bottleneck finding - Flame graph analysis
Talk about the 8 pits of redis distributed lock
Interviewer: how to solve the problem of a large number of requests for data that does not exist in redis, which affects the database?
聊聊写代码的20个反面教材
2020-11-10
父组件加scoped有时也会影响子组件