【发布时间】:2013-01-21 12:06:52
【问题描述】:
是的,所以我正在玩弄内存移动数据。我在这里遇到了麻烦。 我做错了什么?我已经考虑了空终止符,但它仍然没有输出我所期望的。
char buff[34] = "I will not do anything like that.";
char * protocol = "abcdefghi";
char data[44];
memcpy(data, protocol, 10);
memcpy(data + 9, buff, 34);
cout << data << endl; //abcdefghiI will not do anything like that.
cout << strlen(data) << endl; // 42
char poin[10];
memcpy(poin, data, 10);
cout << poin << endl; //abcdefghiI╠╠╠╠╠╠╠╠╠╠abcdefghiI will not do anything like that.
对于最后一个 cout,我期待的只是 abcdefghi,但结果如您所见。 任何帮助表示赞赏,谢谢!
【问题讨论】:
-
我不建议在 C++ 中使用 strcpy 来复制字符串,但使用 memcpy 完全是另一个级别的 C 与类。我会推荐一本好书:stackoverflow.com/q/388242/46642
-
顺便说一句,答案很简单“不,你没有考虑空终止符”
标签: c++ arrays memory character memcpy