C Tutorial/Function/Variable Number of Arguments
Версия от 14:21, 25 мая 2010; (обсуждение)
va_arg, va_copy, va_start, and va_end
Item Value Header file stdarg.h Declartion type va_arg(va_list argptr, type);void va_copy(va_list target, va_list source);void va_end(va_list argptr);void va_start(va_list argptr, last_parm);
Use va_arg(), va_start(), and va_end() macros together to pass a variable number of arguments to a
function. For example, printf(). The type va_list is defined by <stdarg.h>.
#include <stdio.h>
#include <stdarg.h>
double sum_series(int num, ...);
/* Variable length argument example - sum a series. */
int main(void)
{
double d;
d = sum_series(5, 0.5, 0.25, 0.125, 0.0625, 0.03125);
printf("Sum of series is %f.\n", d);
return 0;
}
double sum_series(int num, ...)
{
double sum=0.0, t;
va_list argptr;
/* initialize argptr */
va_start(argptr, num);
/* sum the series */
for( ; num; num--) {
t = va_arg(argptr, double); /* get next argument */
sum += t;
}
/* do orderly shutdown */
va_end(argptr);
return sum;
}