【发布时间】:2014-03-24 01:49:30
【问题描述】:
我只是从 C++ 开始,所以在这里我可能会犯一个愚蠢的错误。下面是我的代码以及 cmets 中的输出。我正在使用 Xcode。
#include <iostream>
#include <string.h>
using namespace std;
int main() {
char myString[] = "Hello There";
printf("%s\n", myString);
strncpy(myString, "Over", 5); // I want this to print out "Over There"
cout<< myString<<endl; // this prints out ONLY as "Over"
for (int i = 0; i <11; i++){
cout<< myString[i];
}// I wanted to see what's going on this prints out as Over? There
// the ? is upside down, it got added in
cout<< endl;
return 0;
}
【问题讨论】:
-
简答:不要使用
strncpy。它是一个只有一个真正目的的功能,所以它的真正用途是非常罕见的。它包含在标准库中几乎完全是历史的偶然。 -
哈哈真有趣....这是我的测验和考试中使用的主要功能之一。
-
听起来是时候解雇你的老师了。 :-)