strcpy、wcscpy、_mbscpy

文字列をコピーします。 これらの関数のセキュリティを強化したバージョンについては、「strcpy_s、wcscpy_s、_mbscpy_s」を参照してください。

char *strcpy(
   char *strDestination,
   const char *strSource 
);
wchar_t *wcscpy(
   wchar_t *strDestination,
   const wchar_t *strSource 
);
unsigned char *_mbscpy(
   unsigned char *strDestination,
   const unsigned char *strSource 
);
template <size_t size>
char *strcpy(
   char (&strDestination)[size],
   const char *strSource 
); // C++ only
template <size_t size>
wchar_t *wcscpy(
   wchar_t (&strDestination)[size],
   const wchar_t *strSource 
); // C++ only
template <size_t size>
unsigned char *_mbscpy(
   unsigned char (&strDestination)[size],
   const unsigned char *strSource 
); // C++ only

パラメーター

  • strDestination
    対象文字列。

  • strSource
    NULL で終わる元の文字列。

戻り値

これらの関数は、コピー先の文字列を返します。 エラーを示す戻り値は予約されていません。

解説

strcpy 関数は、終端の null 文字を含めて、strSource を、strDestination で指定された位置にコピーします。 コピー元とコピー先の文字列が重なり合っている場合の strcpy 関数の動作は未定義です。

セキュリティに関するメモセキュリティに関するメモ

strcpy は、strSource をコピーする前に strDestination に十分な領域があるかどうかを確認しないため、バッファー オーバーランが発生する可能性があります。 代わりに、strcpy_s の使用を検討してください。

wcscpy 関数と _mbscpy 関数は、strcpy 関数のワイド文字バージョンとマルチバイト文字バージョンです。 wcscpy 関数の引数と戻り値はワイド文字列で、_mbscpy 関数の引数と戻り値はマルチバイト文字列です。 それ以外では、これらの関数の動作は同じです。

C++ では、これらの関数にテンプレートのオーバーロードがあります。このオーバーロードは、これらの関数に対応するセキュリティで保護された新しい関数を呼び出します。 詳細については、「セキュリティ保護されたテンプレート オーバーロード」を参照してください。

汎用テキスト ルーチンのマップ

TCHAR.H のルーチン

_UNICODE および _MBCS が未定義の場合

_MBCS が定義されている場合

_UNICODE が定義されている場合

_tcscpy

strcpy

_mbscpy

wcscpy

必要条件

ルーチン

必須ヘッダー

strcpy

<string.h>

wcscpy

<string.h> または <wchar.h>

_mbscpy

<mbstring.h>

互換性の詳細については、「C ランタイム ライブラリ」の「互換性」を参照してください。

使用例

// crt_strcpy.c
// compile with: /W3
// This program uses strcpy
// and strcat to build a phrase.

#include <string.h>
#include <stdio.h>

int main( void )
{
   char string[80];

   // Note that if you change the previous line to
   //   char string[20];
   // strcpy and strcat will happily overrun the string
   // buffer.  See the examples for strncpy and strncat
   // for safer string handling.

   strcpy( string, "Hello world from " ); // C4996
   // Note: strcpy is deprecated; consider using strcpy_s instead
   strcat( string, "strcpy " );           // C4996
   // Note: strcat is deprecated; consider using strcat_s instead
   strcat( string, "and " );              // C4996
   strcat( string, "strcat!" );           // C4996
   printf( "String = %s\n", string );
}
  

同等の .NET Framework 関数

System::String::Copy

参照

参照

文字列操作 (CRT)

strcat、wcscat、_mbscat

strcmp、wcscmp、_mbscmp

strncat、_strncat_l、wcsncat、wcsncat_l、_mbsncat、_mbsncat_l

strncmp、wcsncmp、_mbsncmp、_mbsncmp_l

strncpy、_strncpy_l、wcsncpy、_wcsncpy_l、_mbsncpy、_mbsncpy_l

_strnicmp、_wcsnicmp、_mbsnicmp、_strnicmp_l、_wcsnicmp_l、_mbsnicmp_l

strrchr、wcsrchr、_mbsrchr、_mbsrchr_l

strspn、wcsspn、_mbsspn、_mbsspn_l