成都创新互联网站制作重庆分公司

c语言中内存拷贝函数,实现内存拷贝函数

编写一个C语言的内存拷贝函数,把源地址的指定长度的数据拷贝到目标地址,考虑8,16,32位数据位宽

函数原型为:

公司主营业务:做网站、网站建设、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。创新互联建站是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。创新互联建站推出蔡家坡免费做网站回馈大家。

void *memcpy(

void *dest,

const void *src,

size_t count

);

其中:

dest:

目标内存缓冲区

src:

源内存缓冲区

count:

需要拷贝的字节数

通过memcpy()函数无法确定目标缓冲区的大小,目标缓冲区一般是一个已分配好空间的指针,在分配该空间时一般要先考虑源内存缓冲区的大小,宁愿多分配些空间(即,一般都要分配得比源缓冲区大),第三个参数为源缓冲区中实际需要拷贝的数据的字节数.

C语言中memcpy函数用法

memset函数用来对一段内存空间全部设置为某个字符,常用于内存空间初始化。将已开辟内存空间

s

的首

n

个字节的值设为值

c

下面是一个例子

#include

stdio.h

#include

string.h

main(){

char

*s="golden

global

view";

clrscr();

memset(s,'g',6);

printf("%s",s);

getchar();

return

0;

}

c语言memcpy函数原型:extern

void

*memcpy(void

*dest,

void

*src,

unsigned

int

count);

用法:#include

string.h

功能:由src所指内存区域复制count个字节到dest所指内存区域。

说明:src和dest所指内存区域不能重叠,函数返回指向dest的指针。

举例:

//

memcpy.c

#include

syslib.h

#include

string.h

main()

{

char

*s="golden

global

view";

char

d[20];

clrscr();

memcpy(d,s,strlen(s));

d[strlen(s)]=0;

printf("%s",d);

getchar();

return

0;

}

函数

strchr()

能:

在一个串中查找给定字符的第一个匹配之处\

法:

char

*strchr(char

*str,

char

c);

程序例:

#include

#include

int

main(void)

{

char

string[15];

char

*ptr,

c

=

'r';

strcpy(string,

"this

is

a

string");

ptr

=

strchr(string,

c);

if

(ptr)

printf("the

character

%c

is

at

position:

%d\n",

c,

ptr-string);

else

printf("the

character

was

not

found\n");

return

0;

}

C语言串拷贝(strcpy)和内存拷贝(memcpy)函数有什么不同?

strcpy()函数只能拷贝字符串。strcpy()函数将源字符串的每个字节拷贝到目录字符串中,当遇到字符串末尾的null字符(\0)时,它会删去该字符,并结束拷贝。 memcpy()函数可以拷贝任意类型的数据。因为并不是所有的数据都以null字符结束,所以你要为memcpy()函数指定要拷贝的字节数。 在拷贝字符串时,通常都使用strcpy()函数;在拷贝其它数据(例如结构)时,通常都使用memcpy()函数。以下是一个使用strcpy()函数和memcpy()函数的例子: #include stdio. h #include string. h typedef struct cust-str {int id ;char last_name [20] ; char first_name[l5];} CUSTREC;void main (void); void main (void){char * src_string = "This is the source string" ; char dest_string[50]; CUSTREC src_cust; CUSTREC dest_cust; printf("Hello! I'm going to copy src_string into dest_string!\n"); / * Copy src_ string into dest-string. Notice that the destination string is the first argument. Notice also that the strcpy() function returns a pointer to the destination string. * / printf("Done! dest_string is: %s\n" , strcpy(dest_string, src_string)) ; printf("Encore! Let's copy one CUSTREC to another. \n") ; prinft("I'll copy src_cust into dest_cust. \n"); / * First, intialize the src_cust data members. * / src_cust. id = 1 ; strcpy(src_cust. last_name, "Strahan"); strcpy(src_cust. first_name, "Troy"); / * Now, Use the memcpy() function to copy the src-cust structure to the dest_cust structure. Notice that, just as with strcpy(), the destination comes first. * / memcpy(dest_cust, src_cust, sizeof(CUSTREC));

C语言 实现一个内存复制函数,可以将指定内存地址复制指定的长度到另一个内存地址。求看着别太复杂的

你需要的这个函数,C语言本来就有:memcpy

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

函数的功能是从源src所指的内存地址的起始位置开始拷贝n个字节到目标dest所指的内存地址的起始位置中。

举个例子:将s中的字符串复制到字符数组d中。

#includestdio.h 

#includestring.h 

int main() 

char*s="GoldenGlobalView"; 

chard[20]; 

memcpy(d,s,(strlen(s)+1)); 

printf("%s",d); 

getchar(); 

return0; 

}


文章名称:c语言中内存拷贝函数,实现内存拷贝函数
分享链接:http://cxhlcq.com/article/hdggdc.html

其他资讯

在线咨询

微信咨询

电话咨询

028-86922220(工作日)

18980820575(7×24)

提交需求

返回顶部