1: When you cannot list the types and numbers of all real parameters passing functions, you can specify a parameter table with a ellipsis.
Void Foo (...);
Void Foo (parm_list ,...);
2: transfer principle of function parameters
Function parameters are accessed in the form of Data Structure: Stack, from right to left. eg:
# Include <iostream>
Void fun (int ,...)
{
Int * temp = &;
Temp ++;
For (INT I = 0; I <A; ++ I)
{
Cout <* temp <Endl;
Temp ++;
}
}
Int main ()
{
Int A = 1;
Int B = 2;
Int c = 3;
Int d = 4;
Fun (4, A, B, C, D );
System ("pause ");
Return 0;
}
Output ::
1
2
3
4
3: Get the parameter specified by the ellipsis
Declare a va_list in the function body, and then useVa_startFunction to obtain the parameters in the parameter list. Call va_end () to complete the operation. Like this sectionCode:
Void testfun (char * pszdest, int destlen, const char * pszformat ,...)
{
Va_list ARGs;
Va_start(ARGs, pszformat );
_ Vsnprintf (pszdest, destlen, pszformat, argS );
Va_end (ARGs );
}
4.Va_startPoint argp to the first optional parameter. Va_arg returns the current parameter in the parameter list and points argp to the next parameter in the parameter list. Va_end clears the argp pointer to null. The function can traverse these parameters multiple timesVa_startAnd end with va_end.
1 ). demonstrate how to use functions with variable parameter numbers in the ANSI standard format
# include
# include
# include
/* The function prototype Declaration requires at least one definite parameter. Note the ellipsis in parentheses */
int demo (char ,...);
void main (void)
{< br> demo ("Demo", "this", "is", "A", "Demo! "," ");
}< br>/* ANSI standard declaration method, the ellipsis in parentheses indicates the optional parameter */
int demo (char MSG ,...)
{< br>/* define the structure of the stored function parameters */
va_list argp;
int argno = 0;
char para;
/* Argp points to the first input optional parameter, and MSG is the final parameter */
Va_start(Argp, MSG );
While (1)
{
Para = va_arg (argp, char );
If (strcmp (para, "") = 0)
Break;
Printf ("parameter # % d is: % s \ n", argno, para );
Argno ++;
}
Va_end (argp );
/* Set argp to null */
Return 0;
}
2) // sample code 1: use of variable parameter functions
# Include "stdio. H"
# 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); // obtain 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 );
Return 0;
}
3) // sample code 2: extension-implement simple variable parameter functions by yourself.
The following is a simple implementation of the printf function. For more information, see <The C programming language>.
# Include "stdio. H"
# Include "stdlib. H"
Void myprintf (char * FMT,...) // a simple implementation similar to printf. // The parameters must be of the int type.
{
Char * parg = NULL; // equivalent to the original va_list
Char C;
Parg = (char *) & FMT; // do not write P = FMT !! Because the address must be set for the // parameter, instead of the value.
Parg + = sizeof (FMT); // equivalent to the originalVa_start
Do
{
C = * FMT;
If (C! = '% ')
{
Putchar (c); // output character as is
}
Else
{
// Output data by formatted characters
Switch (* ++ FMT)
{
Case 'D ':
Printf ("% d", * (int *) parg); break;
Case 'X ':
Printf ("% # X", * (int *) parg ));
Break;
Default:
Break;
}
Parg + = sizeof (INT); // equivalent to the original va_arg
}
++ FMT;
} While (* FMT! = '\ 0 ');
Parg = NULL; // equivalent to va_end
Return;
}
Int main (INT argc, char * argv [])
{
Int I = 1234;
Int J = 5678;
Myprintf ("the first test: I = % d \ n", I, j );
Myprintf ("The secend test: I = % d; % x; j = % d; \ n", I, 0 xabcd, J );
System ("pause ");
Return 0;
}