memcpy - C++ Reference

函数
头文件<cstring>

memcpy

void * memcpy ( void * 目标, const void * 源, size_t num(字节大小) );
拷贝整块的内存

拷贝num字节的数据从源到目标指针指向的内存块

这种对象的底层要求源和目标的指针是不同的,拷贝的结果是二进制

这个函数不会检查源中的终止标志,总是拷贝num个字节
为了避免溢出,num的大小应该是源和目标的大小的最小值,并且两者不应重叠


Parameters

destination
Pointer to the destination array where the content is to be copied, type-casted to a pointer of type  void*.
source
Pointer to the source of data to be copied, type-casted to a pointer of type  const void*.
num
Number of bytes to copy.
size_t is an unsigned integral type.

Return Value

destination is returned.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/* memcpy example */
#include <stdio.h>
#include <string.h>

struct {
  char name[40];
  int age;
} person, person_copy;

int main ()
{
  char myname[] = "Pierre de Fermat";

  /* using memcpy to copy string: */
  memcpy ( person.name, myname, strlen(myname)+1 );
  person.age = 46;

  /* using memcpy to copy structure: */
  memcpy ( &person_copy, &person, sizeof(person) );

  printf ("person_copy: %s, %d \n", person_copy.name, person_copy.age );

  return 0;
}


Output:

person_copy: Pierre de Fermat, 46 

See also

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值