There are some mistakes in it, watch it.
1.va_start () va_end () function application (http://www.daydreaming.com.cn/article/2007-5-31/1838-1.htm)
1: When you cannot list the type and number of all arguments for the pass-through function , you can specify the parameter table with ellipses
void foo (...);
void foo (parm_list,...);
2: Transfer principle of function parameters
function parameters are accessed in the form of data structures: stacks, from right to left into stacks. Eg:
#include <iostream>
void Fun (int a, ...)
{
int *temp = &a;
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: Gets the argument specified by the ellipsis
Declare a va_list in the body of the function , and then use the Va_start function to get the arguments in the argument list, and then call Va_end () end after use. Like this piece of 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 makes ARGP point to the first optional parameter. Va_arg returns the current argument in the argument list and causes ARGP to point to the next argument in the argument list. Va_end the ARGP pointer to null. The function body can traverse these parameters multiple times, but must start with va_start and end with Va_end.
1). Demonstrates how to use the function with variable number of parameters, using ANSI standard form
#include 〈stdio.h〉
#include 〈string.h〉
#include 〈stdarg.h〉
/* Function Prototype declaration requires at least one definite parameter, note the ellipsis in parentheses */
int demo (char *, ...). . &NBSP;
void main (void)
{
Demo ("Demo", "This", "is", "a", "demo!", ""); ;
}&NBSP
/*ansi A standard form of declaration, with an ellipsis in parentheses indicating an optional parameter */
Int demo (char *msg, ...) &NBSP
{
* Defines the structure that holds the function parameter */
Va_ List argp;
int argno = 0;
char *para;
/*ARGP points to the first optional parameter passed in, MSG is the last determined parameter.
Va_start (ARGP, msg);
while (1)
{
Para = Va_arg (ARGP, char);
if (STRCMP (Para, "") = = 0)
Break
printf ("Parameter #%d is:%s", Argno, para);
argno++;
}
Va_end (ARGP);
/* Set ARGP to null*/
return 0;
}
2)//Example code 1: variable ParametersfunctionThe use
#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); Determines the memory start address of a parameter, starting with the address of a fixed argument.
Todo
{
++nargcout;
printf("The%d th arg:%d", nargcout,nargvalue); Output values for each parameter
Nargvalue = Va_arg (arg_ptr,int); Gets 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)//Example code 2: extension--self-implementation of simple variable parametersfunction。
The following is a simpleprintf functionThe implementation of the <the C programming language> in reference to the example
#include "stdio.h"
#include "Stdlib.h"
void myprintf (char* fmt, ...) A simple one similar to theprintfimplementation,//parameter must all be int type
{
char* Parg=null; Equivalent to the original va_list
char c;
PARG = (char*) &fmt; Be careful not to write P = fmt!! Because you want to address the//parameter here, not the value
PARG + + sizeof (FMT); Equivalent to the original Va_start
Todo
{
c =*fmt;
if (c!= "%")
{
Putchar (c); Output characters as-is
}
Else
{
Output data by format character
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 test:i=%d", i,j);
myprintf ("The Secend test:i=%d; %x;j=%d; ", i,0xabcd,j);
System ("pause");
return 0;
}
2.vsprintf, vswprintf with printf and variable parameter programming for functions (http://blog.csdn.net/9527/archive/2008/05/19/2457816.aspx)
In C language programming, we inevitably have to contact with variable parameter function , for the C language that does not support function polymorphism, using variable parameters and macro definition function is a good way to realize function polymorphism. Before we go further to the variable parameter function , let's look at the typical two variable parameters that are commonly used, namely vsprintf and sprintf.
First, vsprintf function
Header file
Stdio.h
Category
Memory and string manipulation routines
Prototype
int vsprintf (char *buffer, const char *format, va_list arglist);
int vswprintf (wchar_t *buffer, const wchar_t *format, va_list arglist);
Description
Writes formatted output to a string.
The V ... printf functions are known as alternate entry points for the ... printf functions. They behave exactly like their ... printf counterparts, but they accept a pointer to a list of arguments instead-a argument list.
VSPRINTF accepts a pointer to a series of arguments, applies to each a format specifier contained in the format string poi nted to through format, and outputs the formatted data to a string. There must be the same number of format specifiers as arguments.
return value
VSPRINTF returns the number of bytes output. In the event of error, VSPRINTF returns EOF.
--contrast translation
Header file
Stdio.h
Classification
Memory and string manipulation
function Prototypes
int vsprintf (char *buffer, const char *format, va_list arglist);
int vswprintf (wchar_t *buffer, const wchar_t *format, va_list arglist);
Describe
Write formatted output to a string
V.. printf The family of functions is. Alternative functions for the family of print functions , they are like. printf function Family, but they accept pointers to argument lists instead of argument lists.
VSPRINTF accepts a pointer to a series of variable parameters, providing each parameter with a formatted definition contained in the form, and outputting the formatted data into a string that must be equal in format definition and number of parameters.
return value
VSPRINTF returns the number of bytes in output, and returns EOF when an error occurs
Second, sprintf function
Header file
Stdio.h
Category
Memory and string manipulation routines
Prototype
int sprintf (char *buffer, const char *format[, argument, ...]);
int swprintf (wchar_t *buffer, const wchar_t *format[, argument, ...]);
Description
Writes formatted output to a string.
Note:for details on format specifiers.
Sprintf accepts a series of arguments, applies to each a format specifier contained in the format string pointed to by Mat, and outputs the formatted data to a string.
sprintf applies the "the" "the" the "the" the "the" the "the", "the" "Second", " There must be the same number of format specifiers as arguments.
return value
On success, SPRINTF returns the number of bytes output. The return value does isn't include the terminating null byte in the count.
On error, SPRINTF returns EOF.
--contrast translation
Header file:
Stdio.h
Header file
Stdio.h
Classification
Memory and string manipulation
function Prototypes
int sprintf (char *buffer, const char *format[, argument, ...]);
int swprintf (wchar_t *buffer, const wchar_t *format[, argument, ...]);
Describe
Write formatted output to a string
Note: For formatting definition specifications, see printf
Sprintf accepts a set of parameters, provides a format definition for each parameter, and outputs formatted data to a string
sprintf gives the first parameter a format definition, the second gives a secondary format definition, and the number of formatted definitions must match the number of parameters
return value
Success, returns the number of bytes in output, and the return value does not contain the number of bytes that terminate null bytes
Error, returning EOF
To make it easier to compare the use of these two functions , a fragment of the program is given below:
Char szbuffer[256];
sprintf (Szbuffer, "Welcome%d,%s", 1, "HI");
ShowMessage (Szbuffer);
vsprintf (Szbuffer, "Welcome%d,%s", 1, "HI"); <-Tip [C + + ERROR] Unit1.cpp: e2034 cannot convert "int" to "void *"
ShowMessage (Szbuffer);
Http://cache.baidu.com/c?m= 9f65cb4a8c8507ed4fece763105392230e54f73a7b8e87027fa3cc1f8e2201011035b5ac27541706d7c07f6606ac494bea8635723d032bb59fcf8c4cc abbe57269d779203541c6171dc46fabdc302fd656924d99a90ee7cbb272c8f29487c85422dd23766df3849c2b7303be19&p= 8b2a964e9c934eae52bfc529520a8e&user=baidu&fm=sc&query=%d6%d8%d0%b4+printf+%ba%af%ca%fd&qid= C4d0e1ad04ef7fa5&p1=1