我想尝试 C memcpy 函数。我有这个代码:
char destination[40];
memcpy(destination, "My favorite destination is...", 11);
printf(destination);
我想将前11个字母复制到目标数组。当我使用printf时,结果是“我的收藏夹2”。为什么?
最佳答案
您会在11个字符的末尾缺少NULL终止符-> Printf只会打印内存中该部分的内容,直到找到NULL终止符为止。
只需添加目的地[11] = 0;
那应该工作:)
关于c - memcpy一个子串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8273919/