#include<stdio.h> #include<stdarg.h> #define N 5 void Stdarg(int a1,...) { va_list argp; int i; int ary[N]; va_start(argp,a1); ary[0]=a1; for(i=1;i<N;i++) ary[i]=va_arg(argp,int); va_end(argp); for(i=0;i<N;i++) printf("%d ",ary[i]); } void main() { Stdarg(5,12,64,34,23); }
|
附录C语言之父所写的权威解释:
Variable Argument Lists:
The header provides facilities for stepping through a list of function arguments of unknown number and type.
Suppose lastarg is the last named parameter of a function f with a variable number of arguments. Then declare within f a variable of type va_list that will point to each argument in turn:
va_list ap;
ap must be initialized once with the macro va_start before any unnamed argument is accessed:
va_start(va_list ap, lastarg);
Thereafter, each execution of the macro va_arg will produce a value that has the type and value of the next unnamed argument, and will also modify ap so the next use of va_arg returns the next argument:
type va_arg(va_list ap, type);
The macro void va_end(va_list ap);
must be called once after the arguments have been processed but before f is exited.
阅读(600) | 评论(0) | 转发(0) |