函式
<cstring>

memcpy

void * memcpy ( void * destination, const void * source, size_t num );
複製記憶體塊
source 指向的位置的 num 個位元組的值直接複製到 destination 指向的記憶體塊。

對於此函式,sourcedestination 指標所指向的物件的底層型別是無關緊要的;結果是資料的二進位制複製。

該函式不檢查 source 中的任何終止空字元——它總是精確複製 num 個位元組。

為避免溢位,destinationsource 引數所指向的陣列的大小應至少為 num 個位元組,並且不應重疊(對於重疊的記憶體塊,memmove 是更安全的方法)。

引數

destination
指向目標陣列的指標,內容將被複制到此處,型別轉換為void*.
source
指向要複製的資料來源的指標,型別轉換為const void*.
num
要複製的位元組數。
size_t 是一個無符號整數型別。

返回值

返回 destination

示例

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;
}

輸出

person_copy: Pierre de Fermat, 46 


另見