-
This thing is basically useless.
va_list arguments;
va_start
You go check the API. You can find the definitions of these two functions!
-
Foreword:
Sometimes we need a function interface that can support variable parameters to improve their generalization ability, such as defining a summation function that sums all the incoming parameters and returns them, and C provides a way to help us achieve this.
Principle:
If we can know the address and type of the previous parameter of the variable parameter list, we can know the first address of the variable parameter list, and then extract the corresponding data according to the type of each parameter. To put it simply, the data in the stack is taken out in turn according to the size of the specified type.
Concrete implementation:Step Analysis:
-
va list is used to access variadics. va start is used to initialize the va list variable. va arg is used to get the next variadic.
va end is used to deinitialize the va list variable. The va arg function takes the parameter of the function to which it is currently pointing according to the currently given type, and increments a pointer inside it to point to the next parameter.
For example, va arg(arguments, char*).
Here we assume that the current parameter is of type char*, and modify the internal pointer to increment sizeof(char*) to point to the next parameter. The value obtained may be pointing"beijing"or"olympic games"pointers.
va_arg(arguments, int)
Suppose the current parameter is of type int and modify the internal pointer to increment sizeof(int) to point to the next parameter. The possible value here is 2000 (both 2000).
Note that this is a very low-level function, and the parameter type specified when calling va arg needs to be the same as when passing it, otherwise it may crash. It should also be noted that since types such as char are converted to int when passed to such functions, you must use int instead of char to extract arguments when calling va arg, as detailed in this article.
-
The variable parameter function in the C language is mainly implemented through the va list macro and several macros of related operations.
1. Macros and descriptions involved:
All relevant macros are defined in .
1. va list: pointer to the variadic list.
4. va end: clears the va list of variable parameters.
The implementation of variable parameter functions is the use of these four macros.
2. Examples:
In practical applications, there are often cases where the maximum value of several numbers is obtained, and when the number is relatively large, it is generally realized through an array, but when the number is small, a max function is generally written. The conventional way is to use a few numbers of max, just write a few parameters, for which you can implement a variadic function, the first parameter specifies how much data is in total, and the subsequent parameters are the values to be compared.
#include
#include
int max(int n, .Variadic functions.
1, 2, 3, and 4 values were tested to find the maximum value.
Test result: 1299
-
1. The three macros in the standard C library are only used to determine the memory address of each parameter in the variadic parameter list, and the compiler does not know the actual number of parameters.
2. In practical application, programmers must consider the method of determining the number of parameters, such as:
This is how the printf function is used to set flags in fixed parameters.
In presetting a special closing flag, that is, entering one more variable parameter, the value of the last variable parameter should be set to this special value when calling, and judging whether the end of the parameter has been reached according to this value in the function body. The ** earlier in this article is to adopt this method.
Either way, the programmer should tell the caller their conventions in the documentation.
The direction in which the stack grows.
The order in which the parameters are put into the stack.
CPU alignment.
The expression of the memory address.
Combined with the source **, we can see that the implementation of va list is determined by , the introduction of intsizeof(n) is determined by , he and together with determine the implementation of va start, and finally the existence of va end is the embodiment of a good programming style, and the pointer that is no longer used is set to null, which can prevent future misoperations.
4. After obtaining the address, combined with the type of parameter, the programmer can handle the parameter correctly.
-
The English expression of variable argument is: variable argument
It is defined with three dots when defining the function'.'representation, separated by a comma from other parameters.
The characteristics of variable parameters are not one-to-one correspondence like fixed parameters, nor do they have fixed parameter types and parameter names like fixed parameters; The number of variadic parameters is not.
Definitely, it can be a single parameter or multiple; Each parameter in a variadic parameter can be of different or the same type; Each parameter of the variadic parameter does not.
The actual name corresponds to it.
It can be seen that the form of the variable parameter is very free and elastic. As a result, it gives gifted programmers more room to imagine and develop.
The following is an introduction to several aspects of variable parameters.
1) The storage form of variable parameters.
As we all know, the parameters of a general function are local variables. Local variables are stacks stored in memory (the so-called stacks: automatically allocated and released by the compiler, storing the parameter values of functions, the values of local variables, etc.).
It behaves like a stack in a data structure. ).Variadics are also stored in the memory stack.
When storing the parameters of the function, the compiler presses the stack one by one from the right to the left of the function's parameters, so as to ensure that the top of the stack is the first parameter of the function's parameters (from left to right).The memory allocation order under the 80x86 platform is from high address memory to low address memory.
statement in the defined variables!
You can use function pointers, such as:
voidfunction(int >>>More
using systme;
using ; >>>More
I don't understand your intentions, so you probably want to use polymorphism. >>>More
The friend function should be described in a class in the form of: >>>More