当前位置:网站首页>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参数的函数
边栏推荐
猜你喜欢
随机推荐
运维安全要了解的二三事
Talk about 12 business scenarios of concurrent programming
在 CDP中使用Iceberg 为数据湖仓增压
STL container -- basic operation of map
线上软件测试培训机构柠檬班与iTest.AI平台达成战略合作
Excel template export of easypoi
markdown笔记以及Typora相关快捷键
Flex flexible layout
What is the employment prospect of software testing? There is a large demand for talents and strong job stability
Usage and examples of vlookup function
redis源码分析 2 迭代器
Easypoi excel multi sheet import
[ES6] explain in detail the common objects and other methods of set and array (full version)
【全网首发】JVM性能问题的自动分析
Wechat applet learning notes
交换机用户模式、特权模式、全局模式、端口模式
[first launch in the whole network] automatic analysis of JVM performance problems
BUUCTF 杂项——二维码
Performance bottleneck finding - Flame graph analysis
【全网首发】主线程异常会导致 JVM 退出?









