strcpy() — ストリングのコピー

フォーマット

#include <string.h>
char *strcpy(char *string1, const char *string2);

言語レベル

ANSI

スレッド・セーフ

はい

説明

strcpy() 関数は、終了ヌル文字を含め、string2string1 で指定された位置にコピーします。

strcpy() 関数は、ヌル終了ストリング上で作動します。関数のストリング引数には、 ストリングの終わりを示すマークであるヌル文字 (\0) が含まれていなければなりません。 長さのチェックは行われません。 string2 がリテラル・ストリングの場合であっても、 string1 値にリテラル・ストリングを使用しないでください。

戻り値

strcpy() 関数は、コピーされたストリング (string1) を指すポインターを戻します。

この例では、source の内容を destination にコピーします。
#include <stdio.h>
#include <string.h>
 
#define SIZE 40
 
int main(void)
{
  char source[SIZE] = "This is the source string";
  char destination[SIZE] = "And this is the destination string";
  char * return_string;
 
  printf( "destination is originally = ¥"%s¥"¥n", destination );
  return_string = strcpy( destination, source );
  printf( "After strcpy, destination becomes ¥"%s¥"¥n", destination );
}
 
/*****************  Output should be similar to:  *****************
 
destination is originally = "And this is the destination string"
After strcpy, destination becomes "This is the source string"
*/