传递可变数量的参数 [英] Passing variable number of arguments around
本文介绍了传递可变数量的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有一个接受可变数量参数的 C 函数:我如何调用另一个需要可变数量参数的函数,并传递进入第一个函数的所有参数?
Say I have a C function which takes a variable number of arguments: How can I call another function which expects a variable number of arguments from inside of it, passing all the arguments that got into the first function?
示例:
void format_string(char *fmt, ...);
void debug_print(int dbg_lvl, char *fmt, ...) {
format_string(fmt, /* how do I pass all the arguments from '...'? */);
fprintf(stdout, fmt);
}
推荐答案
要传递省略号,您可以像往常一样初始化 va_list
并将其传递给您的第二个函数.您不使用 va_arg()
.具体;
To pass the ellipses on, you initialize a va_list
as usual and simply pass it to your second function. You don't use va_arg()
. Specifically;
void format_string(char *fmt,va_list argptr, char *formatted_string);
void debug_print(int dbg_lvl, char *fmt, ...)
{
char formatted_string[MAX_FMT_SIZE];
va_list argptr;
va_start(argptr,fmt);
format_string(fmt, argptr, formatted_string);
va_end(argptr);
fprintf(stdout, "%s",formatted_string);
}
这篇关于传递可变数量的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文