strcpy, wcscpy, _mbscpy

文字列をコピーします。 これらの関数のセキュリティを強化したバージョンを使用できます。「strcpy_swcscpy_s_mbscpy_s」を参照してください。

重要

_mbscpy は、Windows ランタイムで実行するアプリケーションでは使用できません。 詳細については、「ユニバーサル Windows プラットフォーム アプリでサポートされていない CRT 関数」を参照してください。

構文

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 関数は、strSource を、終端の NULL 文字も含めて、strDestination で指定された場所にコピーします。 コピー元とコピー先の文字列が重なり合っている場合の strcpy 関数の動作は未定義です。

重要

strcpy が、strDestination をコピーする前に、strSource に十分な領域があるかどうかを確認しないことが、バッファー オーバーランの潜在的な原因です。 したがって、代わりに strcpy_s の使用をお勧めします。

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

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

既定では、この関数のグローバル状態の適用対象は、アプリケーションになります。 この動作を変更するには、「CRT のグローバル状態」を参照してください

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

TCHAR.H ルーチン _UNICODE_MBCS が定義されていない _MBCS が定義されている _UNICODE が定義されている
_tcscpy strcpy _mbscpy wcscpy

必要条件

ルーチンによって返される値 必須ヘッダー
strcpy <string.h>
wcscpy <string.h> または <wchar.h>
_mbscpy <mbstring.h>

互換性の詳細については、「 Compatibility」を参照してください。

// 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];

   // 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; use strcpy_s instead
   strcat( string, "strcpy " );           // C4996
   // Note: strcat is deprecated; use strcat_s instead
   strcat( string, "and " );              // C4996
   strcat( string, "strcat!" );           // C4996
   printf( "String = %s\n", string );
}
String = Hello world from strcpy and strcat!

関連項目

文字列操作
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