memmove

オブジェクトを指定文字数分コピーします.

#include <string.h>
void *memmove(
    void *s1,
    const void *s2,
    size_t n
);

memmove 関数は s2 が指すオブジェクトから,s1 が指すオブジェクトに n 文字分文字をコピーします.

memcpy 関数とは異なり,memmove 関数は一時的な配列に s2 が指すオブジェクトの内容を n 文字コピーした後,その一時的な配列から s1 が指すオブジェクトに n 文字コピーします. これにより領域の重なり合うオブジェクト間でコピーが行われるときの動作は正しく行われます.

戻り値

  • s1 の値

C言語サンプルプログラム

memmove 関数を使用してオブジェクトをコピーするサンプルプログラムを以下に示します.

/* header files */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* main */
int main(void) {
    char s1_1[10] = {'\0'};
    char s1_2[] = "123456789";
    char s1_3[] = "123456789";
    char s2[] = "copy!";

    /* copy1 */
    memmove(s1_1, s2, 5);
    printf("copy1: %s\n", s1_1);

    /* copy2 */
    memmove(s1_2, s2, 5);
    printf("copy2: %s\n", s1_2);

    /* copy3 (overlap domain) */
    memmove(s1_3, s1_3 + 3, 3);
    printf("copy3: %s\n", s1_3);

    return EXIT_SUCCESS;
}

実行例

サンプルプログラムの実行結果は以下のようになります.

copy1: copy!
copy2: copy!6789
copy3: 456456789