oli*_*dev 2 c++ variadic-functions
如果我有这个功能:
printAll(const char *message, ...)
{
va_list argptr = NULL;
va_start(argptr, message);
// todo: how to printf all the arguments in the message?
va_end(argptr);
}
Run Code Online (Sandbox Code Playgroud)
假设我像这样调用函数:
printAll("My info: Value1 = %d, Value 2=%d", 1, 2);
Run Code Online (Sandbox Code Playgroud)
在这一行:// todo:如何printf消息中的所有参数?
我怎样才能打印出来以便:
My info: Value1 = 1, Value 2=2
Run Code Online (Sandbox Code Playgroud)
提前致谢.
Fat*_*ror 15
您正在寻找vprintf()旨在实现此功能的功能:
vprintf(message, argptr);
Run Code Online (Sandbox Code Playgroud)
该v*printf()系列函数以同样的方式作为其正常的同行基本工作,除他们采取的va_list可变参数来代替的.他们不会打电话va_end()给你,所以你现在的方式是正确的.