Detailed explanation of C language variable parameter list (stdarg header file and its defined macros)

        Foreword: When some functions are defined, the parameters in () are not all function parameters are unique, such as the definition of the printf function

The ellipsis here means that the parameters of the printf function can be changed, and the parameters of some functions are fixed, such as strlen

The reason for this difference is mainly because the functions of the functions are different. The printf function needs to print multiple different types of data, while the strlen function calculates the length of the string, and only needs to pass the address of the string. Using the variable parameter list can solve our needs very well, and the realization of the variable parameter list is realized through the header file <stdarg>, which will be introduced in detail below;

2. Introduction to stdarg header files

        Basic concept: define macros to handle functions of unknown type and unknown number of parameters

         How to use: Add ... after the basic parameters to represent the unknown type and the number of unknown parameters

Introduction to va_list

  Prototype:               typedef char* va_list;

    va_list is a pointer to a variable parameter list (note: it is a pointer to the entire variable parameter list, and does not point to the first parameter, pointing to the first parameter needs to be achieved through va_start)

        va_list arg; defines a variable arg of data type va_list for the operation of variable parameter list

Introduction to va_start

        原型:            void va_start(va_list ap, last_arg);

         Specifically: va_start has two parameters, the first is a variable of the va_list data type (that is, a pointer to the variable parameter list), and the second is the nearest parameter before the variable parameter list (that is, ...previous)

        Function: Initialize the variable parameter list, point the pointer to the first parameter of the variable parameter list, so that the program can access each parameter of the variable parameter list one by one:

        va_start(arg,num); num is the function parameter I set myself (the most recent parameter before ...)

    

Introduction to va_arg

        

        Usage: Get each parameter of the variable parameter list and return it, and point the pointer to the next parameter at the same time;

        va_arg(arg,double);

Introduction of va_end

        

         Function: end access to the variable parameter list and release resources

2. A simple example (finding the average of any parameter)

#include <stdio.h>
#include <stdarg.h>
/*函数声明部分*/
double average(int num, ...)
{
    /*定义一个可变参数列表*/
    va_list valist;

    /*初始化可变参数列表,使其指向可变参数列表的第一个参数*/
    va_start(valist, num);

    int i = 0;
    double sum = 0.0;//用于存储总和
    for (i = 0; i < num; i++)
    {
        /*访问可变参数列表的每一个值,并将其返回*/
        sum += va_arg(valist, double);
    }

    /*清理valist所占用的内存*/
    va_end(valist);
    return sum / num;
}
int main()
{
    /*存储任意参数*/
    double a = average(10, 12.0, 23.0, 34.0, 45.0, 56.0, 76.0, 32.0, 33.0, 11.0, 90.0);
    double b = average(5, 22.3, 44.2, 14.5, 66.3, 99.5);
    printf("平均值=%.2lf\n", a);
    printf("平均值=%.2lf\n", b);
    return 0;
}

Summary: Steps to Design a Program Using Variable Argument Lists

1. Write the corresponding function, add fixed parameters and ... in the function parameter part to represent the variable parameter list;

2. Define a variable of type va_list as a variable parameter list;

3. Use va_start to initialize the variable parameter list, so that the pointer points to the first parameter of the variable parameter list;

4. Use va_arg to continuously access each parameter of the variable parameter list, obtain its parameters, and point to the next parameter at the same time;

5. Use va_end to clean up the memory of the variable parameter list and release resources;

        Tip: Be sure to pay attention to the format when using it. The name of the variable parameter list can be directly followed by va_list; the first parameter of va_start( , ) is the name of the list, and the second is the fixed parameter in the function (such as num in the above code) ;va_arg( , ) the first parameter is the list name, the second parameter is the data type of the variable parameter; va_end() parameter is the list name;

Guess you like

Origin blog.csdn.net/Mylvzi/article/details/130693975
Recommended