【发布时间】:2012-05-27 05:17:49
【问题描述】:
我在这里缺少什么?快把我逼疯了!
我有一个返回 const char*
的函数const char* Notation() const
{
char s[10];
int x=5;
sprintf(s, "%d", x);
return s;
}
现在在代码的另一部分我正在这样做:
.....
.....
char str[50];
sprintf(str, "%s", Notation());
.....
.....
但 str 保持不变。
如果我这样做:
.....
.....
char str[50];
str[0]=0;
strcat(str, Notation());
.....
.....
str 设置正确。
我想知道为什么 sprintf 没有按预期工作......
【问题讨论】:
-
也许一个想法是将函数更改为:void Notation(char* buffer) const 并在调用者提供的 char 缓冲区上工作。
-
为什么投反对票?问题很明确,给出了一个“工作”示例,显示了努力,并给出了实际的问题示例。
标签: c++ string pointers constants printf