strncpy,strcpy,memcpy的区别

本文详细对比了strcpy和memcpy两个标准C库函数的功能和使用场景。strcpy专门用于字符串复制,会自动复制结束符;memcpy则可以复制任何类型的内存数据,长度由第三个参数决定。文章还深入分析了它们之间的主要区别,并提供了代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

strcpy和memcpy都是标准C库函数,它们有下面的特点:
strcpy提供了字符串的复制。即strcpy只用于字符串复制,并且它不仅复制字符串内容之外,还会复制字符串的结束符。

已知strcpy函数的原型是:char* strcpy(char* dest, const char* src);
memcpy提供了一般内存的复制。即memcpy对于需要复制的内容没有限制,因此用途更广。
void *memcpy( void *dest, const void *src, size_t count );

char  * strcpy ( char  * dest, const  char  * src) // 实现src到dest的复制
{
   if  ((src == NULL) || (dest == NULL)) //判断参数src和dest的有效性
  {
 
       return  NULL;
  }
   char  *strdest = dest;        //保存目标字符串的首地址
   while  ((*strDest++ = *strSrc++)!= '\0' ); //把src字符串的内容复制到dest下
   return  strdest;
}
void  * memcpy ( void  *memTo, const  void  *memFrom, size_t  size)
{
   if ((memTo == NULL) || (memFrom == NULL)) //memTo和memFrom必须有效
          return  NULL;
   char  *tempFrom = ( char  *)memFrom;             //保存memFrom首地址
   char  *tempTo = ( char  *)memTo;                  //保存memTo首地址     
   while (size -- > 0)                //循环size次,复制memFrom的值到memTo中
          *tempTo++ = *tempFrom++ ; 
   return  memTo;
}

strcpy和memcpy主要有以下3方面的区别。
1、复制的内容不同。strcpy只能复制字符串,而memcpy可以复制任意内容,例如字符数组、整型、结构体、类等。
2、复制的方法不同。strcpy不需要指定长度,它遇到被复制字符的串结束符"\0"才结束,所以容易溢出。memcpy则是根据其第3个参数决定复制的长度。
3、用途不同。通常在复制字符串时用strcpy,而需要复制其他类型数据时则一般用memcpy

memcpy

Summary
#include <string.h>

void *memcpy (
  void *dest,   /* destination buffer */
  void *src,    /* source buffer */
  int len);     /* bytes to copy */
Description

The memcpy function copies len bytes from src to dest. If these memory buffers overlap, the memcpy function cannot guarantee that bytes in src are copied to dest before being overwritten. If these buffers do overlap, use the memmove function.

Return Value

The memcpy function returns dest.

See Also

memccpy, memchr, memcmp, memmove, memset

Example
#include <string.h>
#include <stdio.h> /* for printf */

void tst_memcpy (void) {
  char src1 [100] = "Copy this string to dst1";
  char dst1 [100];
  char *p;

  p = memcpy (dst1, src1, sizeof (dst1));
  printf ("dst = \"%s\"\n", p);
}

strcpy

Summary
#include <string.h>

char *strcpy (
  char *dst,    /* destination string */
  char *src);   /* source string */
Description

The strcpy function copies characters from src to dst up to and including the terminating null character.

Return Value

The strcpy function returns dst.

See Also

strcat, strlen, strncat, strncpy

Example
#include <string.h>
#include <stdio.h> /* for printf */

void tst_strcpy (void) {

  char buf [21];
  char s [] = "Test String";

  strcpy (buf, s);
  strcat (buf, " #2");

  printf ("new string is %s\n", buf);
}

strncpy

Summary
#include <string.h>

char *strncpy (
  char *dst,   /* destination string */
  char *src,   /* source string */
  int len);    /* max characters to copy */
Description

The strncpy function copies at most len characters from src to dst. Characters are copied until a null character ('\0') is copied or until len characters have been copied. If the length of src is less than len the remaining bytes in dst are padded with null characters ('\0').

Return Value

The strncpy function returns dst.

See Also

strcat, strcpy, strlen, strncat

Example
#include <string.h>
#include <stdio.h> /* for printf */

void tst_strncpy (char *s) {
  char buf [21];

  strncpy (buf, s, sizeof (buf));
  buf [sizeof (buf) -1] = '\0';
}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值