当前位置:网站首页>C语言进阶第45式:函数参数的秘密(下)
C语言进阶第45式:函数参数的秘密(下)
2022-04-21 18:11:00 【东川君】
参数入栈顺序
函数参数的计算次序是依赖编译器实现的,那么函数参数的入栈次序是如何确定的呢?

调用约定
当函数调用发生时
–参数会传递给被调用的函数;
–而返回值会被返回给函数调用者;
调用约定描述参数如何传递到栈中以及栈的维护方式
–参数传递顺序;
–调用栈清理;

调用约定是预定义的可理解为调用协议
调用约定通常用于库调用和库开发的时候
-从右到左依次入栈: _stdcall , _cdecl, _thiscall
-从左到右依次入栈︰ _pascal , _fastcall


#include <stdio.h>
float average(int array[], int size)
{
int i = 0;
float avr = 0;
for(i=0; i<size; i++)
{
avr += array[i];
}
return avr / size;
}
int main()
{
int array[] = {1, 2, 3, 4, 5};
printf("%f\n", average(array, 5));
return 0;
}
可变参数
C语言中可以定义参数可变的函数
参数可变函数的实现依赖于stdarg.h头文件
- va_list-参数集合
- va_arg-取具体参数值
- va_start 一标识参数访问的开始
- va_end -标识参数访问的结束
#include <stdio.h>
#include <stdarg.h>
float average(int n, ...)
{
va_list args;
int i = 0;
float sum = 0;
va_start(args, n);
for(i=0; i<n; i++)
{
sum += va_arg(args, int);
}
va_end(args);
return sum / n;
}
int main()
{
printf("%f\n", average(5, 1, 2, 3, 4, 5));
printf("%f\n", average(4, 1, 2, 3, 4));
return 0;
}
可变参数的限制
可变参数必须从头到尾按照顺序逐个访问;
参数列表中至少要存在一个确定的命名参数;
可变参数函数无法确定实际存在的参数的数量;
可变参数函数无法确定参数的实际类型;
注意:
va_arg 中如果指定了错误的类型,那么结果是不可预测的。
小结
1、调用约定指定了函数参数的入栈顺序以及栈的清理方式;
2、可变参数是C语言提供的一种函数设计技巧;
3、可变参数的函数提供了一种更方便的函数调用方式;
4、可变参数必须顺序的访问,无法直接访问中间的参数值;
版权声明
本文为[东川君]所创,转载请带上原文链接,感谢
https://blog.csdn.net/Bruce_Qee/article/details/124323060
边栏推荐
- autoware.auto高精地图 Lanelet2资料汇总
- Laxcus distributed operation redundancy and fault tolerance node
- Integer面试解析、equals与==的区别
- 【今晚七点】metaRTC的发展和应用场景探讨
- 每周推荐短视频:企业之间的竞争是看谁能抢占先机
- Why do infrastructure engineers prefer MySQL?
- 单片机diy作品鉴赏,初学者进来膜拜
- Google has been unable to collect the transformation target on the mobile terminal. What's the matter?
- TCP/IP协议
- 终于有人讲明白了!原来这才是全球低时延一张网技术
猜你喜欢

教你轻松解决CSRF跨站请求伪造攻击

中介者模式
![[AI and food] a review paper on the latest](/img/c3/eb91dbcd2b60c6706b0c6f021267cb.png)
[AI and food] a review paper on the latest "dietary assessment based on visual analysis" by the Institute of computing, Chinese Academy of industry and Commerce

终于完成学生时代的梦想-制作掌机用单片机STM32手把手教你

上位机这样玩,才有意思!

After reading this tutorial, you will have your own satellite (DIY full explanation)

Finally someone made it clear! It turns out that this is the global one-piece network technology with low delay

Pytorch数据封装进入网络前的几种方式
![[npj | digital medicine] machine learning of medical images: failure of methodology and suggestions for the future](/img/8e/918e4e500a62be5ba5480829a32081.png)
[npj | digital medicine] machine learning of medical images: failure of methodology and suggestions for the future

Look at how the technology house saves Xueba machine. I repaired my laptop with 10 yuan
随机推荐
人员不足、供应链断裂,危机之下制造业该如何自救?
西北工业大学机考《财政金融法》网考
Appium source code analysis of dry goods app automated testing
关于Linq语句
Shallow comparison between oceanbase and tidb - implementation plan
[npj | digital medicine] machine learning of medical images: failure of methodology and suggestions for the future
MYCAT horizontal sub table (E-R table)
你必须懂也可以懂的微服务系列三:服务调用
What can MCU do? Do you have any interesting works made by MCU or open source hardware
Do we media sidelines really earn tens of thousands a month? This article is shared without privacy
Kubernetes详解(三)——Kubernetes集群组件
终于完成学生时代的梦想-制作掌机用单片机STM32手把手教你
You must understand and can understand microservice series 3: service invocation
[7:00 pm tonight] discussion on the development and application scenarios of metartc
WPF学习笔记——概述
Summary of mongodb user permissions
单片机能做什么,你有什么有单片机或开源硬件做的有意思的作品吗
【今晚七点】metaRTC的发展和应用场景探讨
Mycat水平分表(全局表)
[刷力扣] 51-60题