memcpy() — バイトのコピー

フォーマット

#include <string.h>
void *memcpy(void *dest, const void *src, size_t count);

言語レベル

ANSI

スレッド・セーフ

はい

説明

memcpy() 関数は、count バイトの srcdest にコピーします。 オーバーラップしたオブジェクト間でコピーが行われる場合には、振る舞いは予期できません。オーバーラップしたオブジェクト間でのコピーは、memmove() 関数で可能です。

戻り値

memcpy() 関数は、dest へのポインターを戻します。

この例では、source のコンテンツを target へコピーします。
#include <string.h>
#include <stdio.h>
 
#define MAX_LEN 80
 
char source[ MAX_LEN ] = "This is the source string";
char target[ MAX_LEN ] = "This is the target string";
 
int main(void)
{
  printf( "Before memcpy, target is ¥"%s¥"¥n", target );
  memcpy( target, source, sizeof(source));
  printf( "After memcpy, target becomes ¥"%s¥"¥n", target );
}
 
/*********************  Expected output:  ************************
 
Before memcpy, target is "This is the target string"
After memcpy, target becomes "This is the source string"
*/