Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C语言memove()函数输出错误?_C_Arrays_Memmove - Fatal编程技术网

C语言memove()函数输出错误?

C语言memove()函数输出错误?,c,arrays,memmove,C,Arrays,Memmove,我不明白为什么我从这段代码中得到这个输出 #include<stdio.h> #include<string.h> int main() { char str[] = "I live in NY city"; printf("%s%s\n","The string in array str[] before invokihg the function memmove(): ",str); printf("%s%s\n","The string

我不明白为什么我从这段代码中得到这个输出

#include<stdio.h>
#include<string.h>
int main()
{
    char str[] = "I live in NY city";

    printf("%s%s\n","The string in array str[] before invokihg the function memmove(): ",str);

    printf("%s%s\n","The string in array str[] before invokihg the function memmove(): ",memmove(str,&str[7],10));
        return 0;
}
这里的输出是:
Sweet Home


根据
memmove()
函数的定义,这是正确的,即从第二个参数指向的对象复制指定数量的字节到第一个参数指向的对象。(使用相同的字符串)这里的object也指一个数据块。

我认为您有一个错误的印象,认为
memmove
应该以某种方式交换目标和源的非重叠部分。这不是它的工作原理。你可能混淆了书中的例子,因为“家”这个词出现了两次。将其更改为“Home Sweet Home”,这样您就可以看到发生了什么,然后阅读memmove的文档/规范,而不是猜测它的作用。

您的期望是错误的

您可以定义一个字符串:

char str[] = "I live in NY city";
然后将10个字节从字符串的结尾移动(复制)到开头:

"I live in NY city\0";
 012345678901234567
       /        /
      /        /
     /        /
    /        /
   /        /
  /        /
"in NY cityNY city\0";

其他的一切都不会被触动。

“这不应该是:在纽约我住的城市吗?”“不。我住的部分怎么会被移动到字符串的末尾呢?我认为你书中的例子不是很好,因为你不能说第一个“家”已经消失,第二个“家”也消失了“已被复制。@molbdnilo是的,定义不是那么简单和混乱的示例使我对这个函数理解错误。不管怎样,C是如何通过H.M.Deelel-Took编程第四版的,你们可以给C++推荐一些吗?不适合初学者,也不适合高级程序员。Smth之间,有更多的例子和实践练习。我的请求的关键字是实践,好的实际例子,简单的英语。@ Stanley The“半官方”StAccOppsC++书籍推荐。现在我看到,它有点从STR(0)到STR(9)的覆盖,从STR(7)开始有10个字节。这个直观的解释是最好的!+这个答案在OP层面上比我写的简短的解释更好。
"I live in NY city\0";
 012345678901234567
       /        /
      /        /
     /        /
    /        /
   /        /
  /        /
"in NY cityNY city\0";