动态没有。论点 [英] Dynamic no. of arguments
问题描述
是否有可能通过一些技巧,或许使用
stdarg的方法,将一个动态(编译时未知)数量的
参数传递给函数?例如,我想枚举一个
值的数组,然后将它们全部传递给sprintf。我不想写一个
显式检查我可能要发送的每个变量,
也不要编写我自己的带有数组的sprintf函数。可以吗?
Is it possible through some trickery, perhaps using the methods of
stdarg, to pass a dynamic (unknown at compile time) number of
arguments to a function? For example, I want to enumerate an array of
values and then pass them all to sprintf. I wouldn''t want to write an
explicit check for each amount of variables that I might want to send,
nor write my own sprintf function that takes an array. Can it be done?
推荐答案
6月10日,8:29 * pm,squaretrian ... @ hotmail.co.uk写道:
On Jun 10, 8:29*pm, squaretrian...@hotmail.co.uk wrote:
是否有可能通过一些技巧,或许使用
stdarg的方法来传递动态(编译时未知)的数量
函数的参数? *例如,我想枚举一个
值的数组,然后将它们全部传递给sprintf。 *我不想写一个
显式检查我可能要发送的每个变量量,
也不要写我自己的sprintf函数,它需要一个数组。 *可以吗?
Is it possible through some trickery, perhaps using the methods of
stdarg, to pass a dynamic (unknown at compile time) number of
arguments to a function? *For example, I want to enumerate an array of
values and then pass them all to sprintf. *I wouldn''t want to write an
explicit check for each amount of variables that I might want to send,
nor write my own sprintf function that takes an array. *Can it be done?
可能,我不能理解你的问题。也许15.3,4
来自C-FAQ会有所帮助:
15.4:如何编写一个可变数量的函数
参数?
答:使用< stdarg.hheader的设施。
这是一个连接一个函数任意数量的
字符串进入malloc''内存:
#include< stdlib.h / * for malloc,NULL,size_t * /
#include< stdarg.h / * for va_ stuff * /
#include< string.h / * for strcat et al。 * /
char * vstrcat(const char * first,...)
{
size_t len;
char * retbuf;
va_list argp;
char * p;
if(first == NULL )
返回NULL;
len = strlen(第一个);
va_start(argp,first);
while((p = va_arg(argp,char *))!= NULL)
len + = strlen(p);
va_end(argp);
retbuf = malloc(len + 1); / * +1用于尾随\0 * /
if(retbuf == NULL)
返回NULL; / *错误* /
(无效)strcpy(retbuf,first);
va_start(argp,first); /* 重新开始;第二次扫描* /
while((p = va_arg(argp,char *))!= NULL)
(void)strcat(retbuf,p) ;
va_end(argp);
返回retbuf;
}
用法类似于
char * str = vstrcat(" Hello,"," world!",(char *)NULL);
注意最后一个参数的演员表;见问题5.2和15.3。
(另请注意,来电者必须释放退回的,malloc''ed
存储空间。)
参考文献:K& R2 Sec。 7.3 p。 155,Sec。 B7 p。 254; ISO秒7.8;
Rationale Sec。 4.8; H& S Sec。 11.4 pp.296-9; CT& P Sec。 A.3 pp。
139-141; PCS Sec。 11 pp.184-5,Sec。 13 p。 242.
15.5:如何编写一个带有格式字符串和
变量数量的参数的函数,比如printf(),并传递它们
printf()完成大部分工作?
答:使用vprintf(),vfprintf()或vsprintf()。
这是一个error()函数,它打印一条错误信息,
前面跟着字符串error:并以换行符结束:
#include< stdio.h>
#include< stdarg.h>
void error(const char * fmt,...)
{
va_list argp;
fprintf(stderr,"错误:");
va_start(argp,fmt);
vfprintf(stderr,fmt,argp);
va_end(argp) ;
fprintf(stderr," \ n");
}
参考文献:K& R2 Sec。 8.3 p。 174,Sec。 B1.2 p。 245; ISO
秒。 7.9.6.7,7.9.6.8,7.9.6.9; H& S Sec。 15.12页,第379-80页; PCS
秒11页.186-7。
Probably, I don''t understand your question properly. Maybe 15.3,4
From the C-FAQ will help:
15.4: How can I write a function that takes a variable number of
arguments?
A: Use the facilities of the <stdarg.hheader.
Here is a function which concatenates an arbitrary number of
strings into malloc''ed memory:
#include <stdlib.h/* for malloc, NULL, size_t */
#include <stdarg.h/* for va_ stuff */
#include <string.h/* for strcat et al. */
char *vstrcat(const char *first, ...)
{
size_t len;
char *retbuf;
va_list argp;
char *p;
if(first == NULL)
return NULL;
len = strlen(first);
va_start(argp, first);
while((p = va_arg(argp, char *)) != NULL)
len += strlen(p);
va_end(argp);
retbuf = malloc(len + 1); /* +1 for trailing \0 */
if(retbuf == NULL)
return NULL; /* error */
(void)strcpy(retbuf, first);
va_start(argp, first); /* restart; 2nd scan */
while((p = va_arg(argp, char *)) != NULL)
(void)strcat(retbuf, p);
va_end(argp);
return retbuf;
}
Usage is something like
char *str = vstrcat("Hello, ", "world!", (char *)NULL);
Note the cast on the last argument; see questions 5.2 and 15.3.
(Also note that the caller must free the returned, malloc''ed
storage.)
References: K&R2 Sec. 7.3 p. 155, Sec. B7 p. 254; ISO Sec. 7.8;
Rationale Sec. 4.8; H&S Sec. 11.4 pp. 296-9; CT&P Sec. A.3 pp.
139-141; PCS Sec. 11 pp. 184-5, Sec. 13 p. 242.
15.5: How can I write a function that takes a format string and a
variable number of arguments, like printf(), and passes them to
printf() to do most of the work?
A: Use vprintf(), vfprintf(), or vsprintf().
Here is an error() function which prints an error message,
preceded by the string "error: " and terminated with a newline:
#include <stdio.h>
#include <stdarg.h>
void error(const char *fmt, ...)
{
va_list argp;
fprintf(stderr, "error: ");
va_start(argp, fmt);
vfprintf(stderr, fmt, argp);
va_end(argp);
fprintf(stderr, "\n");
}
References: K&R2 Sec. 8.3 p. 174, Sec. B1.2 p. 245; ISO
Secs. 7.9.6.7,7.9.6.8,7.9.6.9; H&S Sec. 15.12 pp. 379-80; PCS
Sec. 11 pp. 186-7.
sq ************ @ hotmail.co.uk 说:
是否有可能通过一些欺骗,也许使用
stdarg的方法,将一个动态(编译时未知)的数量传递给一个函数?
参数?
Is it possible through some trickery, perhaps using the methods of
stdarg, to pass a dynamic (unknown at compile time) number of
arguments to a function?
将值放入数组中,并将指针(到数组的第一个
元素)传递给函数,有计数。在函数本身中,使用
a循环来处理数据。
Put the values in an array, and pass a pointer (to the array''s first
element) to the function, along with a count. In the function itself, use
a loop to process the data.
例如,我想枚举一个数组
值,然后将它们全部传递给sprintf。我不想写一个
显式检查我可能要发送的每个变量,
也不要编写我自己的带有数组的sprintf函数。可以吗?
For example, I want to enumerate an array of
values and then pass them all to sprintf. I wouldn''t want to write an
explicit check for each amount of variables that I might want to send,
nor write my own sprintf function that takes an array. Can it be done?
编写一个使用上面给出的技术来接收数据的包装器,并且
将它传递给循环中的sprintf。要确保你有足够的空间来支付
存储字符串中的所有数据。
-
Richard Heathfield< http://www.cpax.org.uk>
电子邮件:-http:// www。 + rjh @
谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>
Usenet是一个奇怪的放置" - dmr 1999年7月29日
Write a wrapper that uses the technique given above to receive the data and
passes it to sprintf in a loop. Be real sure you have enough space to
store all the data in the string.
--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
6月11日上午8:29,squaretrian ... @ hotmail.co.uk写道:
On Jun 11, 8:29 am, squaretrian...@hotmail.co.uk wrote:
是否有可能通过一些技巧,或许使用
stdarg的方法来传递动态(编译时未知)的数量
参数一个功能?例如,我想枚举一个
值的数组,然后将它们全部传递给sprintf。我不想写一个
显式检查我可能要发送的每个变量,
也不要编写我自己的带有数组的sprintf函数。可以吗?
Is it possible through some trickery, perhaps using the methods of
stdarg, to pass a dynamic (unknown at compile time) number of
arguments to a function? For example, I want to enumerate an array of
values and then pass them all to sprintf. I wouldn''t want to write an
explicit check for each amount of variables that I might want to send,
nor write my own sprintf function that takes an array. Can it be done?
从您的要求来看,似乎vsnprintf可以为您实现
你的技巧。
Whiel使用v * f系列函数,需要注意的一件事
是
va_start()和va_end()必须明确调用。忘记
电话
va_end()可能导致未定义的行为。
引用手册:
函数vprintf(),vfprintf(),vsprintf(),vsnprintf()
相当于func-
tions printf(),fprintf(), sprintf(),snprintf(),
,除了它们是用va_list而不是可变数量的
参数调用的
。这些函数没有
调用va_end宏。因此,呼叫后ap的价值是未定义的。
应用程序之后应该调用va_end(ap)。
From your requirements, it looks like vsnprintf can do the trick for
you.
Whiel using the v*f family of functions, one thing to be taken care of
is
va_start() and va_end() has to be called explicitly. Forgetting to
call
va_end() may result in undefined behavior.
Quoting the manual:
The functions vprintf(), vfprintf(), vsprintf(), vsnprintf()
are equivalent to the func-
tions printf(), fprintf(), sprintf(), snprintf(),
respectively, except that they are
called with a va_list instead of a variable number of
arguments. These functions do not
call the va_end macro. Consequently, the value of ap is
undefined after the call. The
application should call va_end(ap) itself afterwards.
这篇关于动态没有。论点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!