技术开发 频道

memcpy函数

【IT168技术文档】 memcpy

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

 用法:#include <string.h>

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

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

 举例:

 // memcpy.c

 view plaincopy to clipboardprint?

 ·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150

 #include <stdio.h>

 #include <string.h>

 int main(int argc, char* argv[])

 {

 char *s="Golden Global View";

 char d[20];

 clrscr();

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

 d[strlen(s)]='\0';

 printf("%s",d);

 getchar();

 return 0;

 }

 截取view

 view plaincopy to clipboardprint?

 ·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150

 #include <string.h>

 int main(int argc, char* argv[])

 {

 char *s="Golden Global View";

 char d[20];

 memcpy(d,s+14,4);

 //memcpy(d,s+14*sizeof(char),4*sizeof(char));也可

 d[5]='\0';

 printf("%s",d);

 getchar();

 return 0;

 }

 输出结果:

 View

 初始化数组

 char msg[10];

 memcpy(msg,0,sizeof(msg));

查看原文地址

0
相关文章