开发者

How can one variable-args function call another? [duplicate]

This question already has answers here: Passing variable number of arguments around (11 answers) Closed 8 years ago.

Say you have 2 functions:

void func(int x,int y,...)
{
 //do stuff
}
void func2(int x,...)
{
 func(x,123,...);
}

How can you make this work, e.g pass the arg-list to the other function?

EDIT: this is a duplicate, can someone merge t开发者_运维技巧hem or whatever?


You need a separate version that works with explicit argument lists:

void vfunc(int x, va_list args)
{
  /* do stuff */
}

void func2(int x, ...)
{
  va_list arg;

  va_start(arg, x);
  vfunc(x, arg);
  va_end(arg);
}

This is the reason there are standard functions like vprintf().


To be added to "unwind" answer:

va_start(3) man page has decent explanation how to work with va_list objects including good example.

Also printf(3) page contains even better example of va_list based call infrastructure around vsnprintf(3) (you can get to this page using any of printf() variants name).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜