C:传递可变数量的参数从一个函数到另一个 [英] C: Passing variable number of arguments from one function to another
本文介绍了C:传递可变数量的参数从一个函数到另一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以,这里的我面对现在的一个小问题 - >我试图写将接受一个char *的消息和参数变量数的函数。我的功能将修改的消息一点点,然后它会调用与消息和给定参数的printf。 Essentialy,我试着写类似的东西:
So, here's a small problem I'm facing right now -> I'm trying to write a function that will accept a char* message and a variable number of arguments. My function will modify the message a little, and then It'll call printf with the message and given parameters. Essentialy, I'm trying to write something like that:
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);
}
因此,任何人知道我应该怎么办做到这一点?我会非常感谢任何帮助:)
So, anybody knows what should I do to make it happen? I'd be most grateful for any help :)
推荐答案
您想使用可变参数...
You want to use varargs...
void modifyAndPrintMessage( char* message, ... )
{
// do somehthing custom
va_list args;
va_start( args, message );
vprintf( newMessage, args );
va_end( args );
}
这篇关于C:传递可变数量的参数从一个函数到另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文