WWW function first variable control need to print the number of string, followed by a number of strings; The next perform a variable control data, followed by a number of variables need to print, such as:
WWW (2, "ABC", "def", 3, a, b, c)
To achieve print ABC def and three variables a, b, c values,
WWW (1, "ABC", 1, a)
To print out the ABC and the value of the variable a,
Pray god to source!
CodePudding user response:
under the variable parameter function reference number ~ ~ ~ ~ ~ ~
Int demo (char * MSG,... )
{
if (! MSG)
{
return 0;
}
int count=0;
Va_list p;
Va_start (p, MSG);
While (true)
{
count++;
Char * s=va_arg (p, char *);
If (STRCMP (s, "")==0)
{
printf("\n");
break;
}
Printf (" % s ", s);
}
Va_end (p);
return count;
}
//test
Int count=demo (" demo ", "This", "is", "a", "demo!" , "");
The normal form of the variable parameter function is as follows:
Return value type function name (type 1 parameter 1, type 2 2 parameters,... Type n, n parameters... );
As shown above, it is a typical style of variable parameter, it consists of n certain parameters, the final... Said the meaning of the variable parameter, it must be noted that... Must be in the final, and it should have at least a certain parameter,
In order to develop variable parameter function, need all documents stdarg. H,
The following is & lt; Stdarg. H> Important inside a few macros are defined as follows:
Typedef char * va_list;
Void va_start (va_list ap, prev_param);/* */ANSI version
Type va_arg (va_list ap, type);
Void va_end (va_list ap);
Va_list is a character pointer, which can be understood as a pointer points to the current parameters, take must be conducted by the pointer,
Va_start () function is to make the va_list type variable pointing to the first variable parameters, so the need to use to... Recently a certain fixed parameters,
This is in the design of variable parameter function requires at least one determine the cause of the parameters,
Then use the va_arg (p, char *), analytic point pData memory area constantly, and to indicate the resolution, in what way is this va_arg () the role of the second parameter,
After the parsing, pData points to the address of a variable parameter, which can get all the value of the variable parameter
CodePudding user response:
What a surprise, didn't really have a great god to source, thank youCodePudding user response: