天天看点

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的区别

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 标签:  库函数 绿色通道:  好文要顶  关注我  收藏该文 与我联系 

strcpy/memcpy/memmove
strcpy/memcpy/memmove

stone Jin

关注 - 5

粉丝 - 10 +加关注 2 0 (请您对文章做出评价) « 博主前一篇: 库函数itoa的C语言实现

» 博主后一篇: static作用(修饰函数、局部变量、全局变量)

继续阅读