当前位置:网站首页>Advanced formula 45 of C language: the secret of function parameters (Part 2)
Advanced formula 45 of C language: the secret of function parameters (Part 2)
2022-04-21 18:15:00 【Dongchuan Jun】
Parameter stack order
The calculation order of function parameters depends on the compiler , So how to determine the stacking order of function parameters ?

Calling convention
When a function call occurs
– Parameters are passed to the called function ;
– The return value is returned to the function caller ;
The calling convention describes how parameters are passed to the stack and how the stack is maintained
– Parameter passing order ;
– Call stack cleanup ;

The calling convention is predefined and can be understood as the calling protocol
Call convention is usually used in library call and library development
- Stack from right to left : _stdcall , _cdecl, _thiscall
- Stack from left to right ︰ _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;
}
Variable parameters
C Functions with variable parameters can be defined in language
The implementation of variable parameter functions depends on stdarg.h The header file
- va_list- Parameter set
- va_arg- Take the specific parameter value
- va_start One identifies the beginning of parameter access
- va_end - Identifies the end of parameter access
#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;
}
Limitation of variable parameters
Variable parameters must be accessed sequentially from beginning to end ;
At least one definite named parameter must exist in the parameter list ;
The variable parameter function cannot determine the number of parameters that actually exist ;
Variable argument function cannot determine the actual type of argument ;
Be careful :
va_arg If the wrong type is specified in , Then the result is unpredictable .
Summary
1、 The calling convention specifies the stacking order of function parameters and the cleaning method of the stack ;
2、 Variable parameters are C Language provides a function design skill ;
3、 Functions with variable parameters provide a more convenient way to call functions ;
4、 Variable parameters must be accessed sequentially , Cannot directly access the intermediate parameter value ;
版权声明
本文为[Dongchuan Jun]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204211810489165.html
边栏推荐
- MySQL 8.0 remote connection 2059 exception
- Teach you to easily solve CSRF Cross Site Request Forgery Attack
- Laravel soar (2. X) - automatically monitor and output SQL optimization suggestions and assist laravel to apply SQL optimization
- 阿里云吴结生:今天的应用架构,正处在一个不可测的阶段
- New ETL scheduling batch management tool taskctl 8.0 simplest installation
- 靶机渗透练习74-DC6
- 随笔小杂记(六)——tqdm进度条显示出现多余行
- About LINQ statements
- Build a short video platform and copy a demo of the login interface
- IoT平台如何实现业务配置中心
猜你喜欢

为什么switch里的case没有break不行

还剩下载次数,可留链接+邮箱

关于Linq语句

In order to offer several big factories, "closed door practice"
![[intensive reading of Thesis] stable linear structures and seam measurements for parallelax image stitching](/img/93/c453cd25883a34d07b2a1b1e6d287c.png)
[intensive reading of Thesis] stable linear structures and seam measurements for parallelax image stitching

你必须懂也可以懂的微服务系列三:服务调用

laravel-soar(2.x) - 自动监控输出 SQL 优化建议、辅助 laravel 应用 SQL 优化

离散数学 命题逻辑

【网络】4G、5G频段汇总

Weekly recommended short video: the competition between enterprises is to see who can seize the first opportunity
随机推荐
Composant tooltip: déterminer si tooltip est affiché en fonction du débordement de contenu
看看项目经理是如何把一个项目带崩的
[npj | digital medicine] machine learning of medical images: failure of methodology and suggestions for the future
靶机渗透练习72-DC4
Laravel soar (2. X) - automatically monitor and output SQL optimization suggestions and assist laravel to apply SQL optimization
Interface
最基本的JDBC模板及数据库乱码处理
【pytorch图像分类】ResNet网络结构
You must understand and can understand microservice series 3: service invocation
MySQL query table field default value
torch.max()的用法
New ETL scheduling batch management tool taskctl 8.0 simplest installation
Q:excel制作折线图
干货|app自动化测试之Andriod WebView如何测试
华为18级大牛整理总结:微服务设计和分布式服务框架原理实践文档
Live app source code, add picture display function on the basis of unable to display pictures
What is the default user name and password of MySQL?
String(一个特殊的数据类型)
Tooltip 組件:根據內容是否溢出判斷是否顯示 Tooltip
Online examination of "financial law" in the computer examination of Northwest University of Technology