C语言 C:将可变数量的参数从一个函数传递到另一个函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15836392/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C: Passing variable number of arguments from one function to another
提问by Ian Traum
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:
所以,这是我现在面临的一个小问题 -> 我正在尝试编写一个函数来接受 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);
}
So, anybody knows what should I do to make it happen? I'd be most grateful for any help :)
那么,有人知道我应该怎么做才能让它发生吗?如有任何帮助,我将不胜感激:)
回答by K Scott Piel
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 );
}
回答by Edward Clements
void modifyAndPrintMessage(char* message,...)
{ char newMessage[1024]; // **Make sure the buffer is large enough**
va_list args;
va_start(args, message);
vsnprintf(newMessage, message, args);
printf(newMessage);
fflush(stdout);
}
回答by msam
回答by Paul
You can use va_listfrom stdarg.h,
您可以使用va_list从stdarg.h,
C example: http://www.tutorialspoint.com/cprogramming/c_variable_arguments.htmC++ example: http://www.cprogramming.com/tutorial/lesson17.html.
C 示例:http: //www.tutorialspoint.com/cprogramming/c_variable_arguments.htmC++ 示例:http: //www.cprogramming.com/tutorial/lesson17.html。
An of course, see the man page: http://linux.die.net/man/3/stdarg
当然,请参见手册页:http: //linux.die.net/man/3/stdarg
Man page example for reference:
供参考的手册页示例:
#include <stdio.h>
#include <stdarg.h>
void
foo(char *fmt, ...)
{
va_list ap;
int d;
char c, *s;
va_start(ap, fmt);
while (*fmt)
switch (*fmt++) {
case 's': /* string */
s = va_arg(ap, char *);
printf("string %s\n", s);
break;
case 'd': /* int */
d = va_arg(ap, int);
printf("int %d\n", d);
break;
case 'c': /* char */
/* need a cast here since va_arg only
takes fully promoted types */
c = (char) va_arg(ap, int);
printf("char %c\n", c);
break;
}
va_end(ap);
}
回答by Parker Kemp
There is a library which includes this functionality. Here is some example code from the reference:
有一个包含此功能的库。以下是参考资料中的一些示例代码:
#include <stdarg.h> /* va_list, va_start, va_arg, va_end */
int FindMax (int n, ...)
{
int i,val,largest;
va_list vl;
va_start(vl,n);
largest=va_arg(vl,int);
for (i=1;i<n;i++)
{
val=va_arg(vl,int);
largest=(largest>val)?largest:val;
}
va_end(vl);
return largest;
}
The ellipsis is actually valid code, and you can use the va_list object to parse a variable number of parameters.
省略号实际上是有效代码,您可以使用 va_list 对象来解析可变数量的参数。

