memcpy一个子串

Nan*_*nik 2 c substring memcpy

我想试试C memcpy函数.我有这个代码:

char destination[40];
memcpy(destination, "My favorite destination is...", 11);
printf(destination);
Run Code Online (Sandbox Code Playgroud)

我想将前11个字母复制到目标数组.当我使用printf时,结果是"我最喜欢的2".为什么?

Ext*_*der 10

您在11个字符的末尾缺少NULL终止符 - > Printf只是打印内存中的任何内容,直到找到NULL终止符.

只需添加目的地[11] = 0;

那应该工作:)