strcpy、wcscpy、_mbscpystrcpy, wcscpy, _mbscpy

复制字符串。Copies a string. 这些函数更安全的版本已发布,请参阅 strcpy_s、wcscpy_s、_mbscpy_sMore secure versions of these functions are available; see strcpy_s, wcscpy_s, _mbscpy_s.


_mbscpy 无法用于在 Windows 运行时中执行的应用程序。_mbscpy cannot be used in applications that execute in the Windows Runtime. 有关详细信息,请参阅通用 Windows 平台应用中不支持的 CRT 函数For more information, see CRT functions not supported in Universal Windows Platform apps.


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  


目标字符串。Destination string.

null 终止的源字符串。Null-terminated source string.

返回值Return Value

其中每个函数都会返回目标字符串。Each of these functions returns the destination string. 没有保留任何返回值以指示错误。No return value is reserved to indicate an error.


strcpy 函数将 strSource(包括终止空字符)复制到 strDestination 指定的位置。The strcpy function copies strSource, including the terminating null character, to the location that's specified by strDestination. 如果源和目标字符串重叠,则 strcpy 的行为是未定义的。The behavior of strcpy is undefined if the source and destination strings overlap.


由于 strcpy 不会在复制 strSource 前检查 strDestination 中空间是否足够,所以这可能会造成缓冲区溢出。Because strcpy does not check for sufficient space in strDestination before it copies strSource, it is a potential cause of buffer overruns. 因此,我们建议你使用 strcpy_sTherefore, we recommend that you use strcpy_s instead.

wcscpy_mbscpy 分别是 strcpy 的宽字符和多字节字符版本。wcscpy and _mbscpy are, respectively, wide-character and multibyte-character versions of strcpy. wcscpy 的参数和返回值是宽字符字符串;而 _mbscpy 的则是多字节字符字符串。The arguments and return value of wcscpy are wide-character strings; those of _mbscpy are multibyte-character strings. 否则这三个函数否则具有相同行为。These three functions behave identically otherwise.

在 C++ 中,这些函数具有模板重载,以调用这些函数的更新、更安全副本。In C++, these functions have template overloads that invoke the newer, secure counterparts of these functions. 有关详细信息,请参阅 Secure Template OverloadsFor more information, see Secure Template Overloads.

一般文本例程映射Generic-Text Routine Mappings

TCHAR.H 例程TCHAR.H routine 未定义 _UNICODE 和 _MBCS_UNICODE & _MBCS not defined 已定义 _MBCS_MBCS defined 已定义 _UNICODE_UNICODE defined
_tcscpy strcpy _mbscpy wcscpy


例程所返回的值Routine 必需的标头Required header
strcpy <string.h><string.h>
wcscpy <string.h> 或 <wchar.h><string.h> or <wchar.h>
_mbscpy <mbstring.h><mbstring.h>

有关其他兼容性信息,请参阅 兼容性For additional compatibility information, see 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!  

请参阅See Also

字符串操作 String Manipulation
strcat、wcscat、_mbscat strcat, wcscat, _mbscat
strcmp、wcscmp、_mbscmp strcmp, wcscmp, _mbscmp
strncat、_strncat_l、wcsncat、_wcsncat_l、_mbsncat、_mbsncat_l strncat, _strncat_l, wcsncat, _wcsncat_l, _mbsncat, _mbsncat_l
strncmp、wcsncmp、_mbsncmp、_mbsncmp_l strncmp, wcsncmp, _mbsncmp, _mbsncmp_l
strncpy、_strncpy_l、wcsncpy、_wcsncpy_l、_mbsncpy、_mbsncpy_l strncpy, _strncpy_l, wcsncpy, _wcsncpy_l, _mbsncpy, _mbsncpy_l
_strnicmp、_wcsnicmp、_mbsnicmp、_strnicmp_l、_wcsnicmp_l、_mbsnicmp_l _strnicmp, _wcsnicmp, _mbsnicmp, _strnicmp_l, _wcsnicmp_l, _mbsnicmp_l
strrchr、wcsrchr、_mbsrchr、_mbsrchr_l strrchr, wcsrchr, _mbsrchr, _mbsrchr_l
strspn、wcsspn、_mbsspn、_mbsspn_lstrspn, wcsspn, _mbsspn, _mbsspn_l