现在的位置: 首页 > 综合 > 正文

memcpy,memccpy,memmove函数

2013年09月26日 ⁄ 综合 ⁄ 共 1629字 ⁄ 字号 评论关闭

函数原型:extern void *memcpy(void *dest, void *src, unsigned int count)

参数说明:dest为目的字符串,src为源字符串,count为要拷贝的字节数。
       
所在库名:#include <string.h>
 
函数功能:将字符串src中的前n个字节拷贝到dest中。
 
返回说明:src和dest所指内存区域不能重叠,函数返回void*指针。

其它说明:暂时无。

实例: 

#include <string.h>
#include 
<stdio.h>

int main()
{
    
char dest[100];
    
char *src="I'm sky2098,please do not call me sky2098!";
    memcpy(dest,src,
13);   //实现字节的复制,注意memcpy返回的是void*类型
    printf("The string dest is:  %s ! ",dest);
   
return 0;
}

在VC++ 6.0  编译运行:

可见,实现了前13个字节的复制。

因为我们为dest初始化分配了100个字节的空间,所以除了我们复制过来的字符,其余的都用“/0”填充,打印出来就像上面的那样。

 

函数原型:extern void *memccpy(void *dest, void *src, unsigned char ch, unsigned int count)

参数说明:dest为目的字符串,src为源字符串,ch为终止复制的字符(即复制过程中遇到ch就停止复制),count为要拷贝的字节数。
       
所在库名:#include <string.h>
 
函数功能:将字符串src中的前n个字节拷贝到dest中,直到遇到字符ch便停止复制。
 
返回说明:src和dest所指内存区域不能重叠,函数返回void*类型指针。

其它说明:暂时无。

实例: 

#include <string.h>
#include 
<stdio.h>

int main()
{
    
char dest[100];
    
char *src="I'm sky2098,please do not call me sky2098!";
    memccpy(dest,src,
'o',strlen("I'm sky2098,please do"));
    printf(
"The string dest is:  %s ! ",dest);
   
return 0;
}

在VC++ 6.0  编译运行:

程序实现了只从src中前strlen("I'm sky2098,please do")个字符中拷贝,如果遇到字符'o'则停止拷贝。

函数原型:extern void *memmove(void *dest, const void *src, unsigned int count)

参数说明:dest为目的字符串,src为源字符串,count为要拷贝的字节数。
       
所在库名:#include <string.h>
 
函数功能:将字符串src中的前n个字节拷贝到dest中。
 
返回说明:src和dest所指内存区域可以重叠,函数返回void*类型指针。

其它说明:功能于memcpy相同。

实例: 

#include <string.h>
#include 
<stdio.h>

int main()
{
    
char dest[100];
    
char *src="I'm sky2098,please do not call me sky2098!";
    memmove(dest,src,strlen(
"I'm sky2098,please do"));
    printf(
"The string dest is:  %s ! ",dest);
   
return 0;
}

在VC++ 6.0  编译运行:

 

实现的功能同memcpy是相同的。

 

抱歉!评论已关闭.