C、处理可变参数函数 [英] C, Dealing with variable argument functions
本文介绍了C、处理可变参数函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我想做这样的事情
void my_printf(char *fmt,...) {
char buf[big enough];
sprintf(buf,fmt,...);
}
将可变数量的参数直接传递给接受可变参数的函数的正确方法是什么?
What is the proper way of passing the variable number of arguments directly to a function with accepts variable arguments?
推荐答案
sprintf
有一个名为 vsprintf
的 va_list
表单.将您在本地构建的 va_list
作为最后一个参数传递给它.
sprintf
has a va_list
form called vsprintf
. Pass the va_list
you construct locally to it as the last argument.
void my_printf(char *fmt,...) {
va_list ap;
va_start(ap, fmt);
char buf[big enough];
vsprintf(buf,fmt,ap);
va_end(ap);
}
这篇关于C、处理可变参数函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文