How do I fill a va_list

ぃ、小莉子 提交于 2020-01-09 10:16:40

问题


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);
} 

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
...

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.


回答1:


There's no ability to fill a va_list explicitly.

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);
}

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.




回答2:


Normally, these functions come in pairs. If you have a "va-accepting" function, it is easy to create another one:

void printInts_v(int n, va_list ap)
{
    unsigned int i=0;
    for(i=0; i<n; i++)
    {
        int arg=va_arg(ap, int);
        printf("%d", arg);
    }
}

This function can be called this way:

void printInts(int n,...)
{
    va_list ap;
    va_start(ap, n);
    printInts_v(n, ap);
    va_end(va);
}

But I don't think there is a way to portably pre-fill a va_list for later use.

If you work on one architecture and portability is not an issue, you could craft something on your own, however. How exactly to do that is platform-specific.




回答3:


I don't think there's a standardized way of doing this. On the other hand, such a pre-filled va_list would be pretty useless, since there's no way of "querying" it for the number and types of available arguments, either.




回答4:


Since other replies tells it's impossible (which would be a good reason to add it in the design of the language for the next version), The only portable possibility would be

  • to check the documentation twice to investigate if you didn't miss or misunderstand something about the framework you're trying to use : I had the exact same problem here and it appeared in the answer that I was not using the right call for what I was trying to do;

  • to write yourself a function which would take an array of pointers as parameter instead of the va_list and split the problem in multiple calls (not always possible); or

  • assume the framework you're trying to use was poorly designed...



来源:https://stackoverflow.com/questions/13703770/how-do-i-fill-a-va-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!