≫ ホーム ≫ C言語ヘッダファイル別一覧 | C言語アルファベット別一覧 ≫ string.h ≫ strcpy
文字列をコピーします.
#include <string.h>
char *strcpy( char * restrict s1, const char * restrict s2 );
strcpy 関数は s2 が指す文字列 (終端ナル文字を含む) を s1 が指す配列にコピーします.
領域の重なり合うオブジェクト間でコピーが行われるときの動作は未定義です.
注意!
strcpy 関数はバッファオーバーフロー (buffer over-flow) を発生させやすい関数の 1 つです.
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!