va_list 传递函数变参到下一个函数的使用方法:

#include<stdio.h>
#include<stdarg.h>
#include <memory.h>
void fun2(int start,va_list ap);
void simple_va_fun(int start,...)
{
     va_list arg_ptr;
     int nArgValue = start;
     int nArgCount = 0;
     va_start(arg_ptr,start);
     fun2(start,arg_ptr);
  va_end(arg_ptr);
 return;
}

void fun2(int start,va_list ap)
{
  int nArgValue = start;
     int nArgCount = 0;
  do
  {
   ++nArgCount;
   printf("the %d the arg:%d/n",nArgCount,nArgValue);
   nArgValue = va_arg(ap,int);
  }
  while (nArgValue != -1);
}

main()
{
    simple_va_fun(100,200,-1);
    simple_va_fun(777,888,999,67567,45678,3245,-1);
}

 

 

以上代码在fedora13下运行通过:

 

zxr@localhost/mnt/hgfs/linuxtmp/src/tstVa_list$./a.out
the 1 the arg:100
the 2 the arg:200
the 1 the arg:777
the 2 the arg:888
the 3 the arg:999
the 4 the arg:67567
the 5 the arg:45678
the 6 the arg:3245
zxr@localhost/mnt/hgfs/linuxtmp/src/tstVa_list$

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值