strcpy vs. memcpy

后端 未结 9 856
梦如初夏
梦如初夏 2020-12-07 08:17

What is the difference between memcpy() and strcpy()? I tried to find it with the help of a program but both are giving the same output.

         


        
9条回答
  •  长情又很酷
    2020-12-07 08:38

    printf("%s",...) stops printing the data when null is encountered, so both the outputs are same.

    The following code differentiates between strcpy and memcpy:

    #include
    #include
    
    int main()
    {
        char s[5]={'s','a','\0','c','h'};
        char p[5];
        char t[5];
        int i;
        strcpy(p,s);
        memcpy(t,s,5);
        for(i=0;i<5;i++)
            printf("%c",p[i]);
            printf("\n");
        for(i=0;i<5;i++)
            printf("%c",t[i]);
    
        return 0;
    }
    

提交回复
热议问题