是的,所以我正在玩弄内存移动数据。我在这里遇到了麻烦。 我做错了什么?我已经考虑了空终止符,但它仍然没有输出我期望的结果。
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.
对于最后一次计算,我只期待 abcdefghi,但结果如上所示。 任何帮助表示赞赏,谢谢!
最佳答案
point
不是以 '\0'
结尾的字符串。您在这里用 'I'
覆盖了第一个 '\0'
:
memcpy(data + 9, buff, 34);
关于c++ - 内存操作 - 我做错了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14438244/