• strcpy和memcpy的异同


    strcpy

    原型

    char *strcpy(char *dest, const char *src);
    
    • 1

    描述

    The  strcpy()  function  copies the string pointed to by src, including the terminating null byte ('\0'), to the buffer  pointed  to  by  dest.
    
    • 1

    strcpy函数会把src的内容复制到dest,并且包含字符串结束符"\0"。

    示例

    #define _CRT_SECURE_NO_WARNINGS
    #include 
    #include 
    #include 
    
    
    
    int main() {
    
        char str1[] = "&";
        char str2[] = "hello";
        strcpy(str2,str1);
        int len = sizeof(str2) / sizeof(str2[0]);
        for (int i = 0; i < len; i++)
        {
            printf("%c\n",str2[i]);
        }
        printf("copy result:%s\n",str2);
    
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    运行结果

    在这里插入图片描述
    PS:为什么最后printf打印的结果是&?因为printf在打印字符串的时候,遇到字符结束符“\0”就会结束,其实复制以后,str2的内容为"&\0llo"

    strncpy

    原型

    char *strncpy(char *dest, const char *src, size_t n);
    
    • 1

    描述

    The  strcpy()  function is similar, except that at most n bytes of src are copied.  Warning: If there is no null byte among the first n  bytes
     of src, the string placed in dest will not be null-terminated.
    
    • 1
    • 2

    和strcpy很相似,只是限制了最多n个bytes会被复制

    示例

      printf("----------------------------use strncpy--------------------------\n");
        char str3[] = "&";
        char str4[] = "hello";
        strncpy(str4,str3,1);
        printf("strncpy真实数据---------------------------------------------------------\n");
      
        int str4len = sizeof(str4) / sizeof(str4[0]);
        for (int i = 0; i < str4len; i++)
        {
            printf("str4:%c\n", str4[i]);
        }
        printf("copy result:str3=%s,str4=%s\n", str3, str4);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    结果

    在这里插入图片描述

    memcpy复制字符串

    原型

    void *memcpy(void *dest, const void *src, size_t n);
    
    • 1

    描述

     The  memcpy()  function  copies  n bytes from memory area src to memory area dest.  The memory areas must not overlap.  Use memmove(3)  if  the memory areas do overlap.
    
    • 1

    memcpy() 函数将 n 个字节从内存区域 src 复制到内存区域 dest。 内存区域不得重叠。 如果内存区域确实重叠,请使用 memmove。

    示例

       char str5[] = "&";
        char str6[] = "hello";
        memcpy(str6,str5,1);
        printf("memcpy真实数据---------------------------------------------------------\n");
        int str6len = sizeof(str6) / sizeof(str6[0]);
        for (int i = 0; i < str6len; i++)
        {
            printf("str6:%c\n",str6[i]);
        }
        printf("memcpy result:str5=%s,str6=%s\n", str5, str6);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    运行结果

    在这里插入图片描述

    memcpy复制结构体

        printf("memcpy复制结构体---------------------------------------------------------\n");
        struct Student
        {
            char name[40];
            int age;
        };
        char std1Name[] = "Hello";
        struct Student std1, std2;
        std1.age = 18;
        memcpy(std1.name,std1Name,sizeof(std1Name)/sizeof(char));
        memcpy(&std2,&std1,sizeof(std1));
        printf("Student2:name=%s,age=%d\n",std2.name,std2.age);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    结果

    在这里插入图片描述

    相同

    1、都位于的头文件中
    2、都可以完成对于字符串的拷贝

    区别

    1、strcpy只可以复制字符串,而memcpy除了复制字符串,还可以复制其他类型的数据
    2、strcpy会复制字符串的结束符"\0",由于memcpy限制了复制的长度,一定程度上限制了字符串结束符的复制

  • 相关阅读:
    使用Python的方式理解Golang的结构体struct
    css实现倾斜按钮(radial-gradient)
    pytest
    Python如何用Numba加速科学计算和数据分析
    Python使用virtualenvwrapper模块创建虚拟环境
    【数据结构基础_图】Leetcode 997.找到小镇的法官
    10个WordPress的query_posts语句使用技巧
    html中的换行(\n)或回车(\r)符号不起作用的解决办法、br、white、space、pre、line
    java文件查看大小,压缩,文件下载工具类
    88Q2110 通过C22方式访问C45 phy地址
  • 原文地址:https://blog.csdn.net/u011557841/article/details/126891412