我希望在C/C++中实现这一点。我遇到了可变长度参数,但这表明了使用利布菲和C的解决方案。
现在,如果我想用myprintf包装printf函数。
我这样做如下:
void myprintf(char* fmt, ...)
{
va_list args;
va_start(args, fmt);
printf(fmt, args);
va_end(args);
}
int _tmain(int argc, _TCHAR* argv[])
{
int a = 9;
int b = 10;
char v = 'C';
myprintf("This is a number: %d and \nthis is a character: %c and \n another number: %d\n", a, v, b);
return 0;
}但结果并不像预期的那样!
This is a number: 1244780 and
this is a character: h and
another number: 29953463我错过了什么?
发布于 2012-04-30 07:55:53
void myprintf(char* fmt, ...)
{
va_ list args;
va_ start(args, fmt);
printf(fmt, args); // This is the fault. "vprintf(fmt, args);"
// should have been used.
va_ end(args);
}如果您只是试图调用printf,有一个名为vprintf的printf变量,它直接接受va_list : vprintf(fmt,args);
https://stackoverflow.com/questions/41400
复制相似问题