内存拷贝函数memcpy

void* __cdecl memcpy(
    _Out_writes_bytes_all_(_Size) void* _Dst,
    _In_reads_bytes_(_Size)       void const* _Src,
    _In_                          size_t      _Size
    );

由src指向地址为起始地址的连续n个字节的数据复制到以dst指向地址为起始地址的空间内。


1.source和destin所指内存区域不能重叠,函数返回指向destin的指针。

2.与strcpy相比,memcpy并不是遇到'\0'就结束,而是一定会拷贝完n个字节。

3.如果目标数组destin本身已有数据,执行memcpy()后,将覆盖原有数据(最多覆盖n)。如果要追加数据,则每次执行memcpy后,要将目标数组地址增加到你要追加数据的地址。

#include <stdio.h>
#include <string.h>
int main_()
{
	const char *s = "Golden Global View";
	char d[25];
	memcpy(d, s, strlen(s));
	d[strlen(s)] = '\0'; //因为从d[0]开始复制,总长度为strlen(s),d[strlen(s)]置为结束符
	printf("%s", d);
	getchar();
	return 0;
}
//output: Golden Global View

int main()
{
	const char* s = "Golden Global View";
	char d[20];
	memcpy(d, s + 14, 4); //从第14个字符(V)开始复制,连续复制4个字符(View)
	//memcpy(d,s+14*sizeof(char),4*sizeof(char));也可
	d[4] = '\0';
	printf("%s", d);
	getchar();
	return 0;
}
//output: View

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值