Analysis of variable parameter list

1. What is a variable parameter?
In C language programming, the number of formal parameters in a function is generally determined. Calling a function is to instantiate these parameters at one time. Sometimes the parameters of the function will be uncertain. At this time, there will be functions with variable number of parameters. For example, using variable parameters, find the maximum value of function parameters. :
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<Windows.h>
#include<stdarg.h> //Variable parameter lists are implemented through macros, which are defined in the stdarg.h header file               
int average( int n, ...)//... is the variable parameter list part
{
va_list arg;//Declare a variable arg of type va_list
int i = 0;
int max = 0;
va_start(arg, n);//va_start to initialize arg, arg points to the first element of the variable parameter
// Implement function size judgment
for (i = 0; i < n; i++)
{
int val = va_arg(arg, int);//The int type is used as a parameter, the purpose is to read the int length value, va_arg is a macro;

if (max < val)
{
max = val;
}
}
return max;
va_end(arg);//When accessing the last variable parameter, you need to call va_end to clean up the parameter list;
}
int main()
{
int a = 13;
int b = 22;
int c = 12;
int num1 = average(3, a, b, c);//When using variable parameters, you should first enter the number of variable parameters, and then pass in the parameters.
printf("%d ", num1);
system("pause");
return 0;

      
As can be seen from the above example, the steps to implement variadic parameters are as follows:

  1) Declare a variable arg of type va_list, arg is a pointer to the first parameter, used to access the undetermined part of the parameter list 
  2) Then initialize the variable arg with the va_start macro, the second parameter of this macro is ' The last named parameter before ...'. 
  3) Use va_arg to access the variable parameter and assign it to val. The second parameter of va_arg is the type of the parameter to be read, which is int here. At the same time va-arg points to the next variadic parameter
  4) Use end, end the variadic parameter with the va_end macro.

About stdarg macros

Variable argument lists are implemented through macros, which are defined in the stdarg.h file. This file declares a type va-list and three macros va-start, va-arg, va-end. The role of the three macros is only to determine the memory address of each parameter in the variable parameter list. The compiler does not Know the actual number of parameters. The "..." in the parameter is used to indicate that an undetermined number and type of parameters may be passed here.

  Precautions for
      variadic parameters ①Variable parameters must be accessed one by one from the beginning to the end, and can be stopped in the middle.
      ②If you want to access the intermediate elements from the beginning, it is not acceptable
    

  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325528223&siteId=291194637