strcpy

文字列をコピーします.

#include <string.h>
char *strcpy(
    char * restrict s1,
    const char * restrict s2
);

strcpy 関数は s2 が指す文字列 (終端ナル文字を含む) を s1 が指す配列にコピーします.

領域の重なり合うオブジェクト間でコピーが行われるときの動作は未定義です.

注意!
strcpy 関数はバッファオーバーフロー (buffer over-flow) を発生させやすい関数の 1 つです.

戻り値

  • s1 の値

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

strcpy 関数を使用して文字列をコピーするサンプルプログラムを以下に示します.

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

/* macros */
#define N 256

/* main */
int main(void) {
    char s1[N] = {'\0'};
    char s2[] = "snoopy!";

    strcpy(s1, s2);
    printf("s1: %s\n", s1);

    return EXIT_SUCCESS;
}

実行例

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

s1: snoopy!