strcpy/memcpy/memmove


#include <memory.h>   
#include <string.h>   
#include <stdio.h>   
    
char string1[60] = "The quick brown dog jumps over the lazy fox";   
char string2[60] = "The quick brown fox jumps over the lazy dog";   

void main( void )   
{   
  printf( "Function:\tmemcpy without overlap\n" );   
  printf( "Source:\t\t%s\n", string1 + 40 );   
  printf( "Destination:\t%s\n", string1 + 16 );   
  memcpy( string1 + 16, string1 + 40, 3 );   
  printf( "Result:\t\t%s\n", string1 );   
  printf( "Length:\t\t%d characters\n\n", strlen( string1 ) );   
    
  /* Restore string1 to original contents */   
  memcpy( string1 + 16, string2 + 40, 3 );   
    
  printf( "Function:\tmemmove with overlap\n" );   
  printf( "Source:\t\t%s\n", string2 + 4 );   
  printf( "Destination:\t%s\n", string2 + 10 );   
  memmove( string2 + 10, string2 + 4, 40 );   
  printf( "Result:\t\t%s\n", string2 );   
  printf( "Length:\t\t%d characters\n\n", strlen( string2 ) );   
    
  printf( "Function:\tmemcpy with overlap\n" );   
  printf( "Source:\t\t%s\n", string1 + 4 );   
  printf( "Destination:\t%s\n", string1 + 10 );   
  memcpy( string1 + 10, string1 + 4, 40 );   
  printf( "Result:\t\t%s\n", string1 );   
  printf( "Length:\t\t%d characters\n\n", strlen( string1 ) );   
}   
    
/*    
  Output   
    
  Function: memcpy without overlap   
  Source: fox   
  Destination: dog jumps over the lazy fox   
  Result: The quick brown fox jumps over the lazy fox   
  Length: 43 characters   
    
  Function: memmove with overlap   
  Source: quick brown fox jumps over the lazy dog   
  Destination: brown fox jumps over the lazy dog   
  Result: The quick quick brown fox jumps over the lazy dog   
  Length: 49 characters   
    
  Function: memcpy with overlap   
  Source: quick brown dog jumps over the lazy fox   
  Destination: brown dog jumps over the lazy fox   
  Result: The quick quick brown dog jumps over the lazy fox   
  Length: 49 characters



 memmove也是把source 指向的对象中的n个字符拷贝到destin所指向的对象中,
 但过程就好象是先把source所指向的对象拷贝到临时数组中,
 然后在从临时数组拷贝到destin所指的对象中,返回指向结果对象的指针。   
  但要注意,除memmove之外的字符串操作函数在拷贝同一个字符串中的字符时,
  其结果是不确定的。
  也就是说,memmove可以把自己的一部分拷贝给自己的另一部分。
  其他函数不行,比如memcpy.   
  */



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

分类:  C Language
标签:  库函数
2
0
(请您对文章做出评价)
« 博主前一篇: 库函数itoa的C语言实现
» 博主后一篇: static作用(修饰函数、局部变量、全局变量)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值