所以,这是我现在面临的一个小问题 -> 我正在尝试编写一个函数来接受 char* 消息和可变数量的参数。我的函数将稍微修改消息,然后它将使用消息和给定参数调用 printf。本质上,我正在尝试写类似的东西:
void modifyAndPrintMessage(char* message,...){
char* newMessage; //copy message.
//Here I'm modifying the newMessage to be printed,and then I'd like to print it.
//passed args won't be changed in any way.
printf(newMessage,...); //Of course, this won't work. Any ideas?
fflush(stdout);
}
所以,有人知道我应该怎么做才能实现它吗?如果有任何帮助,我将不胜感激 :)
最佳答案
你想使用可变参数...
void modifyAndPrintMessage( char* message, ... )
{
// do somehthing custom
va_list args;
va_start( args, message );
vprintf( newMessage, args );
va_end( args );
}
关于C:将可变数量的参数从一个函数传递到另一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15836392/