Variable parameters are expressed in English as variable argument. When defining a function, it is represented by the three periods "." and separated by commas (,) and other parameters. Variable parameters: Unlike Fixed parameters, they do not have fixed parameter types and parameter names; the number of variable parameters is not fixed, but the input parameter can also be multiple. The type of each parameter in the variable parameter can be different or the same; each parameter of a variable parameter does not correspond to the actual name.
It can be seen that the form of variable parameters is very free and flexible. Because, it gives those geniusesProgramMembers have more space to imagine and use. However, more freedom also increases the operational difficulty. The following describes several aspects of variable parameters.
1) storage of variable parameters.
As we all know, common function parameters belong to local variables. Local variables are stored in the stack area of the memory (the so-called stack area: the compiler automatically allocates and releases the local variables, and stores the function parameter values. The operation method is similar to the stack in the data structure .). Variable parameters are also stored in the memory stack. When storing the function parameters, the compiler stacks them one by one from the right to the left of the function parameters, this ensures that the top of the stack is the first parameter of the function parameter (from left to right ). On the 80x86 platform, the memory allocation sequence is from high-address memory to low-address memory. Therefore, the form of the function parameter stored in the memory is as follows (take Fun (INT var1, int var2,..., int var3, int var4 ):
Stack zone:
| Stack top and low address
| The first fixed parameter var1
| Var2, the first fixed parameter before a Variable Parameter
| The first parameter of a Variable Parameter
| ...
| Last Variable Parameter
| The second-to-last fixed parameter var3 of the Function
| The last fixed parameter var4 of the Function
| ...
| Return address of the Function
| ...
| High stack address
2) header files and macro descriptions used for variable parameters
Here, the tc2.0 compiler is used as a reference object. Variable parameters are defined in the header file named "stdarg. H" of tc2.0. This file is:
/* Stdarg. h
Definitions for accessing parameters in functions that accept
A variable number of arguments.
Copyright (c) Borland International 1987,1988
All rights reserved.
*/
# If _ Stdc __
# Define _ Cdecl
# Else
# Define _ Cdecl
# Endif
# If ! Defined (_ stdarg)
# Define _ Stdarg
Typedef Void * Va_list;
# Define Va_start (AP, parmn) (AP = ...)
# Define Va_arg (AP, type) (* (type *) (AP) ++)
# Define Va_end (AP)
# Define _ Va_ptr (...)
# Endif
The above content is "stdarg. H.
This file defines the data type used by variable parameters: typedef void * va_list;
Va_start (AP, parmn) is used for initialization. The AP points to the first parameter of a variable parameter.
The AP type is va_list.
Parmn is a fixed parameter before a variable parameter.
Va_arg (AP, type) obtains the parameters that the current AP points to, and points the AP to the next parameter of a variable parameter.
Type is the type of the parameter to be obtained.
The va_end (AP) end variable parameter is obtained.
3) Instances for variable parameters
Instance objective: to use a variable parameter to transmit a variable number of strings and display the passed strings.
# Include < Stdio. h >
# Include < Conio. h >
# Include < Stdarg. h >
Void Tvararg ( Int Num ,...); /* Num is the number of variable parameters. */
Int Main ( Void )
{
Clrscr ();
Tvararg ( 5 , " Hello! " , " My " , " Name " , " Is " , " Neverthesame. \ n " );
Tvararg ( 8 , " This " , " Is " , " An " , " Example " , " About " , " Variable-argument " , " In " , " Funtion " );
Getch ();
Return 0 ;
}
Void Tvararg ( Int Num ,...)
{
Va_list argp; /* Define a variable pointing to a variable parameter */
Va_start (argp, num ); /* Initialize and use argp to point to the first parameter of a variable parameter. */
While ( -- Num > = 0 )
Printf ( " % S " , (Va_arg (argp, Char * ))); /* Va_arg (argp, char *) obtains the parameter pointed to by argp,
And use argp to point to the next parameter. char * uses the obtained Of The type of the parameter is converted to char. */ Va_end (argp ); /* End variable parameter acquisition */ Return;
}
4) Notes for using variable parameters
1. Each function has at most one variable parameter.
2. In va_start (AP, parmn), parmn is a fixed parameter before the variable parameter.
3. The number of variable parameters is uncertain. It is entirely agreed by the program.
4. The variable parameter type is unknown. It is completely specified by the type in va_arg (AP, type), and the type of the parameter is forcibly converted. But does printf () Implement the identification parameter? That is because the function printf () analyzes the parameter type from the fixed format string, and then calls va_arg to obtain the variable parameter. That is to say, if you want to implement Intelligent Identification of variable parameters, you must make judgments in your own programs.
5. the compiler does not strictly check the prototype of the Variable Parameter Function and has high requirements on programmers.