重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

C语言复制内存的函数 c语言调用内存

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

函数原型为:

创新互联公司主要从事成都做网站、网站制作、网页设计、企业做网站、公司建网站等业务。立足成都服务淄川,十余年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:18982081108

void *memcpy(

void *dest,

const void *src,

size_t count

);

其中:

dest:

目标内存缓冲区

src:

源内存缓冲区

count:

需要拷贝的字节数

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

C语言 实现一个内存复制函数,可以将指定内存地址复制指定的长度到另一个内存地址。求简单容易看懂的

memcpy

语法:

#include string.h

void *memcpy( void *to, const void *from, size_t count );

功能:函数从from中复制count 个字符到to中,并返回to指针。 如果to 和 from 重叠,则函数行为不确定。

memmove

语法:

#include string.h

void *memmove( void *to, const void *from, size_t count );

功能: 与mencpy相同,不同的是当to 和 from 重叠,函数正常仍能工作。

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语言调用内存
标题来源:http://cqcxhl.cn/article/dososoj.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP