Variable Parameter functions in C Language
Stdarg. h
Stdarg. h is the header file of the C standard function library. It aims to enable the function to receive indefinite parameters. This header file must be included when variable parameter functions need to be defined.
Let's start with an example. The idea of this example comes from
#include <stdio.h>#include <stdarg.h>enum drink{apple = 3,banana = 4,orange = 5,other = 6};int count(int n,...){ int total = 0; va_list ap; va_start(ap,n); for (int i = 0; i < n; ++i) { total += (int) va_arg(ap, enum drink); } va_end(ap); return total;}int main(){ printf("cost %d\n",count(3,apple,orange,other)); printf("cost %d\n",count(5,apple,apple,banana,other,orange));}
This example defines a type of beverage and sets the price of the beverage.
Then, a function count is defined to calculate the price. The first parameter indicates the number of purchases, and then the purchased variety (variable). The total cost is obtained.
// An indefinite parameter function must have at least one named parameter.
Output:
Cost 14
Cost 21
Program ended with exit code: 0
Let's talk about the principle:
Since a function has at least one named parameter, a benchmark is available to define a pointer to locate the starting address of a subsequent parameter based on the address of the benchmark variable.
If you know the type of subsequent parameters, you can obtain the parameter value through the pointer based on the length of the parameter type.
Next we will introduce the usage of the four macros:
Va_list: defines the pointer above.
Va_start: locate the address of the subsequent variable. Since there must be at least one variable parameter (multiple parameters can be included), you can locate the starting address of the subsequent parameter based on the last known parameter.
Va_arg: extracts data by pointer based on the type of subsequent parameters.
Va_end: complete some work
Finally, Let's explain the previous example:
First defines a pointer va_list ap;
Use the va_start function to associate the pointer ap with the known parameter n and locate the starting address of the subsequent parameter.
After positioning, use the va_arg function. The pointer ap and parameter type are used to obtain data. Because the first parameter in this example is the number, the number of times the for loop is to be summed is known.
End va_end.
Appendix:
Variable Parameter Learning notes
Variable parameters in C Language
You can consider this problem as follows:
You have called the vfprintf () function in the write_log () function. In fact, this vfprintf () function is a function that accepts Variable Parameter strings passed from upper-layer functions.
Now you want to call the write_log () function under the log_info () function and pass the variable parameter string to it. You just need to refer to the vfprintf () function definition to define write_log () function.
In C language, the vfprintf () function is defined:
Int vfprintf (FILE * stream, const char * format, va_list ap );
I wonder if you will be inspired.
How can I determine the number of parameters in a function that calls variable parameters such as printf or a self-defined function in C language?
Printf reads data from formatted strings.
Printf ("% d % s % d", omitted); % d % s % d is in the format
Custom Variable Parameter Function, custom parsing, from va_list