When I read "unix network programming" today, I see its error handling function err_sys () and other definitions as follows:
Void
Err_sys (const char * fmt ,...)
{
Va_list ap;
Va_start (ap, fmt );
Err_doit (1, LOG_ERR, fmt, ap );
Va_end (ap );
Exit (1 );
}
What does va_start and va_end do? I have never used it. Google on the internet, originally:
--- This function functions start -----------------
Declare a va_list in the function body, and use the va_start function to obtain the parameters in the parameter list. Call va_end () to complete the use. Like this Code:
Void TestFun (char * pszDest, int DestLen, const char * pszFormat ,...)
{
Va_list args;
Va_start (args, pszFormat );
_ Vsnprintf (pszDest, DestLen, pszFormat, args );
Va_end (args );
}
--- This function serves end -----------------
It turns out that.
Copy the google Article below:
(Description: from [http://www.daydreaming.com.cn/article/2007-5-31/1838-1.htm ])
Va_start () va_end () function application
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 use the va_start function to obtain the parameters in the parameter list. Call va_end () to complete the use. Like this Code:
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_start points 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 times, but all parameters must start with va_start and end with va_end.
1) demonstrate how to use functions with variable parameter numbers in the ANSI standard format
# Include <stdio. h> 〉
# Include <string. h> 〉
# Include <stdarg. h> 〉
/* The function prototype Declaration requires at least one definite parameter. Note the ellipsis in brackets */
Int demo (char ,...);
Void main (void)
{
Demo ("DEMO", "This", "is", "a", "demo! ","");
}
/* ANSI standard declaration method. The ellipsis in parentheses indicates an optional parameter */
Int demo (char msg ,...)
{
/* Define the structure for saving 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 original va_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;
}
Thank you "!