一、memcpy()
- <string.h>
- 函数原型:void *memcpy(void *str1, const void *str2, size_t n) ;
- 从存储区 str2 复制 n 个字节到存储区 str1;
- 返回值:该函数返回一个指向目标存储区 str1 的指针;
#include <stdio.h>
#include <string.h>
int main ()
{
const char src[50] = "http://www.runoob.com";
char dest[50];
memcpy(dest, src, strlen(src)+1);
printf("dest = %s\n", dest);
return(0);
}
结果:
dest = http:
#include <stdio.h>
#include<string.h>
int main()
{
char *s="http://www.runoob.com";
char d[20];
memcpy(d, s+11, 6);
d[6]='\0';
printf("%s", d);
return 0;
}
结果:
runoob
#include<stdio.h>
#include<string.h>
int main(void)
{
char src[] = "***";
char dest[] = "abcdefg";
printf("使用 memcpy 前: %s\n", dest);
memcpy(dest, src, strlen(src));
printf("使用 memcpy 后: %s\n", dest);
return 0;
}
结果:
使用 memcpy 前: abcdefg
使用 memcpy 后: ***defg