Document directory
Va_list
Type to holdinformation about Variable Arguments
This type is used as a parameter for the macros definedin
Cstdarg to retrieve the additional arguments of a function.
Type va_list is used to retrieve the parameters attached to a function and use them as macro parameters defined in cstdarg.
Each compiler may implement this type in its own way. itis only intended to be used as the type for the object used as first argumentfor
Va_start,
Va_arg and va_endmacros.
Va_startis in charge of Initializing an object of this type, so that subsequent callsto
Va_arg with it retrieve the additional arguments passed tothe function.
Each compiler uses its own method to implement this type. We only need to use it as the first parameter of va_start, va_arg, and va_end. Va_start initializes an object of this type, and then va_arg is responsible for retrieving other additional parameters passed to the function.
Before a function that has initialized
Va_listobject with va_start returns,
Va_endshall be executed.
After a function uses va_start to initialize the va_list object, va_end should be executed first.
Va_start
void va_start ( va_list ap, paramN );
Initialize a variable argument list: initialize a variable parameter list
Initializes the objectof type va_list passed asargument
APTo hold the information needed to retrieve the additionalarguments after Parameter
ParamnWith function va_arg.
Initialization refers to the object of the type passed in by the parameter AP as va_list. this object will be used to retrieve the information of additional parameters after paramn in the va_arg function.
A function that executes va_start, shall also execute
Va_end before it returns.
A function that executes va_start should also execute va_end before returning.
Parameters
AP: object of Type va_list that will hold theinformation needed to retrieve the additional arguments
Va_arg.
Paramn: parametername of the last named parameter in the function definition.
The parameter name of the last parameter in the function definition.
/* va_start example */#include <stdio.h>#include <stdarg.h>void PrintFloats ( int amount, ...){ int i; double val; printf ("Floats passed: "); va_list vl; va_start(vl,amount); for (i=0;i<amount;i++) { val=va_arg(vl,double); printf ("\t%.2f",val); } va_end(vl); printf ("\n");}int main (){ PrintFloats (3,3.14159,2.71828,1.41421); return 0;}
/* va_arg example */#include <stdio.h>#include <stdarg.h>int FindMax ( int amount, ...){ int i,val,greater; va_list vl; va_start(vl,amount); greater=va_arg(vl,int); for (i=1;i<amount;i++) { val=va_arg(vl,int); greater=(greater>val)?greater:val; } va_end(vl); return greater;}int main (){ int m; m= FindMax (7,702,422,631,834,892,104,772); printf ("The greatest one is: %d\n",m); return 0;}
/* va_arg example */#include <stdio.h>#include <stdarg.h>void PrintLines ( char* first, ...){ char* str; va_list vl; str=first; va_start(vl,first); do { printf ("%s\n",str); str=va_arg(vl,char*); } while (str!=NULL); va_end(vl);}int main (){ PrintLines ("First","Second","Third","Fourth",NULL); return 0;}