看到简化的代码 - 我很困惑...printf()的困惑 - 印刷(空)时,试图打印字符串,并打印缓冲区时,打印正确的值
#include <stdio.h>
#include <string>
#include <cstdlib> // includes the standard library and overarchs over stdlib.h
using namespace std;
void main()
{
char buffer[10];
string theString;
int i = 997799; //(simplified)
itoa(i,buffer,10);
theString = buffer;
printf("\n string is: %s of length %d \n", theString, theString.length());
printf("\n buffer is: %s of length %d \n", buffer, theString.length());
return;
}
我得到的输出是意外:
string is: (null) of length 926366009
buffer is: 997799 of length 6
(1)为什么字符串打印为空? (2)为什么theString.length()在第一个printf()中没有正确打印,但在第二个中是正确的? (3)如果我在Visual Studio中设置了断点,'buffer'显示为“997799”,而'theString'显示为{“997799”} - 这里有一些奇怪的事情发生?
谢谢大家! 编辑我非常感谢提供答案的详细程度 - 他们都更加清楚,并帮助我超越了我的问题 - 非常感谢你花帮忙:)
'void main'不合法。使用'int main'。 'itoa'不是标准功能。并且http://stackoverflow.com/questions/10865957/c-printf-with-stdstring – chris