Reflections on the change of the growth parameters of C (reproduced)
Thoughts on the change of Cere of Cemes (experienced)
In some cases, the number of parameters of the function is desired can be determined as needed. Typical examples have a familiar function Printf (), scanf () and system calling execl (), etc. So how do they achieve? The C compiler typically provides a series of macros that handle this situation, which is different from the different hardware platform, and increase the portability of the program. These macros include VA_START, VA_ARG, and VA_END, and the like.
---- When using an ANSI standard form, the prototype statement of the variable number of variable numbers is:
Type Funcname (Type Para1, Type Para2, ...)
---- This form requires at least one common form parameter, which does not mean omitted, but part of the function prototype. TYPE is the type of function return value and form parameters.
---- When using the Unix System v compatible statement, the number of variable variable variables is:
TYPE FUNCNAME (VA_ALIST)
VA_DCL
---- This form does not need to provide any common form parameters. TYPE is the type of function return value. VA_DCL is a detailed declaration of parameter VA_ALIST in the function prototype declaration, actually a macro definition, which is defined by different types of different hardware platforms, but in the end, a semicolon is included. So the VA_DCL will no longer need to add a semicolon. VA_DCL must be given in the code. VA_ALIST can be given in the VC, or it can be omitted.
---- The program written by the header file stdarg.h is in line with the ANSI standard, which can be run on various operating systems and hardware; while using the header file varargs.h is only compatible with the previous program. So I suggest that you will use the former. The following mainly describes the processing of parameters in the previous way. The basic principles of the two ways are consistent, but there are some subtle differences in the form of grammar.
---- va_start makes ArgP point to the first optional parameter. VA_ARG Returns the current parameter in the parameter list and causes ArgP to point to the next parameter in the parameter list. VA_END seizes the ArgP pointer as NULL. These parameters can be traversed multiple times in the function, but must start with VA_START and end with VA_END.
---- The caller When the number of variable numbers actually calls the number of parameters, the number of actual parameters should be specified by a certain method, such as setting the last parameter as an empty string (system call execl () is like this) , -1 or other modes (function printf () is the number of actual parameters) by the first parameter, i.e., the output format is defined.
---- A specific example is given below. It is a code that meets the ANSI standard. Add some comments in the code, this is no longer explained here. This example has been compiled and running in VC / Windows XP, CC / AIX4.3.2.0, GCC / SUSE7.3 environments.
---- 1, demonstrate how to use a variable number of variables, using ansi standard form
#include
#include
#include
/ * Function prototype declaration, at least one determined parameter,
Note the omitting number * /
INT Demo (char *, ...);
Void main (void)
{
Demo ("Demo", "THIS", "IS", "A", "Demo!", "/ 0");
}
/ * ANSI standard form of declaration, omitted number indicating optional parameters * /
Int demo (char * msg, ...)
{
VA_LIST Argp; / * Defines the structure of the save function parameters * /
INT Argno = 0; / * record parameters * / char * para; / * Store the string parameter * /
/ * Argp points to the first optional parameter of incoming,
MSG is the last determined parameter * /
VA_START (Argp, MSG);
While (1) {
PARA = VA_ARG (Argp, Char *); / *
Remove the current parameters, type char *. * /
IF (strCMP (PARA, "/ 0") == 0)
/ * End end * /
Break;
Printf ("Parameter #% D IS:% S / N", Argno, Para);
Argno ;
}
VA_END (ARGP); / * Set Argp to null * /
Return 0;
}