当前位置:网站首页>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参数的函数
边栏推荐
- [first launch in the whole network] automatic analysis of JVM performance problems
- Cesium 綁定鼠標事件和移除鼠標事件
- 性能瓶颈查找-火焰图分析
- STL container -- basic operation of map
- Rxjs source code analysis (I) observable
- 【Es6】forEach,for... in ,for... Of column, which allows you to quickly distinguish the usage and differences of various for statements through project cases (full version). There are detailed notes ins
- Round robin schedule problem
- ambari集群扩容节点+扩容服务操作
- 一次全面的性能优化,从5秒优化到1秒
- Continue from the previous issue: the remaining two methods of the rotation chart
猜你喜欢

Redis source code analysis - data structure and Implementation (Dictionary dict)

mysql的锁

网络命令:网卡信息,netstat,arp

Wechat applet learning notes

Parent components plus scoped sometimes affect child components

Data visualization

第一个智能合约程序Faucet.sol

Single arm routing configuration

Cesium BIND Mouse Events and remove Mouse Events

MySQL cache strategy and solution
随机推荐
手把手教你复现Log4j2核弹级漏洞
Easypoi之excel简单导出
The latest news of spring recruitment in 2022: the average salary of it Internet industry is 18500 yuan
Is the cookie valid for a limited time? How to set cookies? Teach you to set by hand
Talk about the 8 pits of redis distributed lock
STL container - basic operation of vector
聊聊并发编程的12种业务场景
Solutions for vscode terminal failure
MySQL - index
Swagger配置与使用
新手学习渗透测试的入门指南
指针进阶简单总结
Easypoi excel multi sheet import
路由器loopback口实验
2.6.2 memory leakage
From 20s to 500ms, I used these three methods
User mode protocol stack - UDP implementation based on netmap
聊聊redis分布式锁的8大坑
Cityengine 3D pipe modeling tutorial
ArcMap 创建常量栅格并镶嵌至新栅格