实例:
//All the variable parameters have the type of "int"
//sum: The only fixed parameter that indicates the number of variable parameters.
//returns the summary of variable parameter.
int sum(int num,...)
{
va_list ap;
int sum = 0;
int i;
//Use the identifier of the rightmost parameter in the variable parameter list
//to initialize ap. ap has the address of the first parameter in the variable parameter list
va_start(ap, num);
//The va_arg macro expands to an expression that has the type and value of
//the next argument in the call. ap will be modified to the address
//of the next parameter. In this case, va_arg will return a value of type int
//and ap will increase to the next int argument.
for (i = 0; i < num; i++)
{
sum += va_arg(ap, int);
}
//The va_end macro facilitates a normal return from the function whose
//variable argument list was referenced by the expansion of va_start
va_end(ap);
return sum;
}
调用:
sum(6,1,2,3,4,5,6)