【发布时间】:2014-06-06 02:51:42
【问题描述】:
char * stft (const char *fmt, ...) {
va_list items;
char *out;
int magic = 0; // <-- here magic?
va_start (items, fmt);
vsprintf (out, fmt, items);
va_end (items);
return out;
}
像这样使用:
char *str = stft ("%s-%s %s", a, b, c);
这是有效的解决方案吗? 如果删除未使用的“魔术”变量-返回字符串后出现分段错误。 做错了什么?
$ gcc --version gcc (Debian 4.4.5-8) 4.4.5
$ unname -a Linux deep-station (squeeze) 2.6.32-5-686 #1 SMP Fri May 10 08:33:48 UTC 2013 i686 GNU/Linux
【问题讨论】:
-
与问题无关,但不应该有初始化
out的东西吗? -
@guest:与问题无关——问题恰恰是
out没有初始化,所以会发生崩溃。 -
不错的侦探。谢谢