The C function must beProgramThe following macros are used:
Void va_start (va_list arg_ptr, prev_param );
Type va_arg (va_list arg_ptr, type );
Void va_end (va_list arg_ptr );
Va here is the meaning of Variable-argument (Variable Parameter.
These macros are defined in stdarg. H, so programs that use variable parameters should include
Header file. Below we will write a simple variable parameter function. The function should have at least one integer.
Parameter. The second parameter is also an integer and is optional. The function only prints the values of these two parameters.
Void simple_va_fun (int I ,...)
{
Va_list arg_ptr;
Int J = 0;
Va_start (arg_ptr, I );
J = va_arg (arg_ptr, INT );
Va_end (arg_ptr );
Printf ("% d/N", I, j );
Return;
}
We can declare our function in our header file as follows:
Extern void simple_va_fun (int I ,...);
We can call it in the program as follows:
Simple_va_fun (100 );
Simple_va_fun (100,200 );
From the implementation of this function, we can see that the following steps should be taken to use variable parameters:
1) first define a va_list variable in the function. Here is arg_ptr. This variable
Volume is a pointer to a parameter.
2) then use the va_start macro to initialize the variable arg_ptr. The second parameter of this macro is
The previous parameter of a variable parameter is a fixed parameter.
3) then return the variable parameter with va_arg and assign the value to the second integer J. va_arg.
The parameter is the type of the parameter you want to return. Here it is the int type.
4) use the va_end macro to end variable parameter acquisition. Then you can make
The second parameter is used. If the function has multiple variable parameters, call va_arg to obtain
Take parameters.
Http://blog.csdn.net/zxianrong/article/details/3706405