Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1008766
  • 博文数量: 327
  • 博客积分: 9995
  • 博客等级: 中将
  • 技术积分: 4319
  • 用 户 组: 普通用户
  • 注册时间: 2009-05-25 11:21
文章存档

2011年(31)

2010年(139)

2009年(157)

我的朋友

分类: C/C++

2009-09-06 09:13:25

#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.
阅读(594) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~