C strcpy和memcpy的异同

strcpy

原型

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

描述

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

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

示例

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>



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;
}

运行结果

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

strncpy

原型

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

描述

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.

和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);

结果

在这里插入图片描述

memcpy复制字符串

原型

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

描述

 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.

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);

运行结果

在这里插入图片描述

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、都位于<string.h>的头文件中
2、都可以完成对于字符串的拷贝

区别

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

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值