1. What are variable parameters?
In C language programming, we sometimes encounter some functions with variable parameter numbers, such as the printf () function. Its prototype is:
Int Printf ( Const Char * Format, ...);
In addition to the fixed format parameter, the number and type of parameters that follow are variable (with three vertices "…" Parameter placeholder), the actual call can be in the following form:
Printf ("% d", I );
Printf ("% s", S );
Printf (" Number Is % D , String Is: % s ", I, S );
2. Write a simple variable parameter C function
First look at the example program. This function has at least one integer parameter, followed by a placeholder ..., The number of subsequent parameters is not fixed. In this example, all input parameters must be integers, and the function is to print the values of all parameters.
The function code is as follows:
// Example code 1: use of variable parameter functions
# Include "stdio. H "# include <iostream> # include" stdarg. H "Void simple_va_fun (INT start ,...) {va_list arg_ptr; int nargvalue = start; int nargcout = 0; // number of variable parameters va_start (arg_ptr, start ); // determine the memory start address of the Variable Parameter Based on the fixed parameter address. Do {++ nargcout; printf ("the % d th Arg: % d \ n", nargcout, nargvalue); // output the values of each parameter nargvalue = va_arg (arg_ptr, INT); // get the value of the next variable parameter} while (nargvalue! =-1); return;} int main (INT argc, char * argv []) {simple_va_fun (100,-1); simple_va_fun (100,200,-1 ); system ("pause"); Return 0 ;}
From the implementation of this function, we can see that the following steps should be taken to use variable parameters:
(1) The following macros will be used in the program:
Typedef char * va_list; // string pointer
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 this header file.
(2) The function first defines a va_list variable arg_ptr, which is a pointer to the parameter address, because the parameter value can be obtained after obtaining the parameter address and combining the parameter type.
(3) Use the va_start macro to initialize the variable arg_ptr defined in (2). The second parameter of this macro is
The previous parameter in the Variable Parameter List, That is
Last fixed parameter.
(4) use the va_arg macro in sequence to make arg_ptr return the address of the Variable Parameter. After this address is obtained, the parameter value can be obtained based on the parameter type. Then output.
The condition indicates whether the parameter value is-1. Note that the called function does not know the correct number of variable parameters when calling. The programmer must specify the end condition in the code. As to why it does not know the number of parameters, the reader will naturally understand after reading the internal implementation mechanisms of the following macros.
Iii. macro definition
Typedef char * va_list; // string pointer
# DEFINE _ intsizeof (N) (sizeof (n) + sizeof (INT)-1 )&~ (Sizeof (INT)-1 ))
# Define va_start (AP, V) (AP = (va_list) & V + _ intsizeof (V) // obtain the starting memory address of the first Variable Parameter
# Define va_arg (AP, t) (* (T *) (AP + = _ intsizeof (t)-_ intsizeof (t ))) // obtain the value of this parameter based on the specified parameter type, and adjust the pointer to the starting address of the next parameter.
# Define va_end (AP) (AP = (va_list) 0)