如何填写的va_list [英] How do I fill a va_list

查看:176
本文介绍了如何填写的va_list的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个va_list的我知道如何提取它的所有元素:

If I have a va_list I know how to extract all its elements:

void printInts(int n,...)
{
    va_list va;
    va_start(va, n);
    for(unsigned int i=0; i<n; i++)
    {
        int arg=va_arg(va, int);
        printf("%d",arg);
    }
    va_end(va);
} 

所以,当我打电话printInts(3,1,2,3)所使用的va_list得到填补所有的参数。结果
但我怎么手工填写不使用的va_start的va_list?我的意思是,我想是这样的:

So when I call printInts(3,1,2,3) the va_list get filled of all the parameters.
But how do I manually fill a va_list without using va_start? I mean that I want something like:

va_list va;
push_arg(va, int, 5); // And so on until I fill all parameters
...

我需要这个,因为有接受的va_list作为参数的函数,我不知道如何来填补它的所有参数的va_list的。

I need this because there is a function that accept a va_list as argument, and I don't know how to fill that va_list of all its parameters.

推荐答案

有没有明确填写的va_list能力。

There's no ability to fill a va_list explicitly.

您应该写一个包装函数。说你需要打电话给你的函数foo,而不是在va_list的手工灌装,你定义像这样一个新功能:

You should write a wrapper function. Say you need to call your function foo, instead of manually filling in a va_list, you define a new function like so:

void call_foo(int arg1, ...)
{
   va_list ap;
   va_start(ap, arg1);
   foo(arg1, ap);
   va_end(ap);
}

现在你可以调用但,这需要的va_list,你喜欢,例如通过做call_foo(1,2,3,4);, call_foo(1,1,你好);等等。

Now you can call foo, which takes a va_list, however you like, by doing e.g. call_foo(1,2,3,4);, call_foo(1, 1, "Hello"); etc.

这将只允许你指定在编译时的参数,你无法在运行时建立的参数。

This will only allow you to specify the arguments at compile time, you can't build the arguments at runtime.

这篇关于如何填写的va_list的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆