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.

语法Syntax

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

参数Parameters

strDestinationstrDestination
目标字符串。Destination string.

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

返回值Return Value

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

备注Remarks

Strcpy函数副本strSource,包括终止 null 字符,由指定的位置strDestinationThe 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不会检查足够空间strDestination复制之前strSource,很可能会造成缓冲区溢出。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分别是宽字符及多字节字符版本的strcpywcscpy 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_tcscpy strcpystrcpy _mbscpy_mbscpy wcscpywcscpy

要求Requirements

例程Routine 必需的标头Required header
strcpystrcpy <string.h><string.h>
wcscpywcscpy <string.h> 或 <wchar.h><string.h> or <wchar.h>
_mbscpy_mbscpy <mbstring.h><mbstring.h>

有关其他兼容性信息,请参阅 兼容性For additional compatibility information, see Compatibility.

示例Example

// 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、_mbscatstrcat, wcscat, _mbscat
strcmp、wcscmp、_mbscmpstrcmp, wcscmp, _mbscmp
strncat、_strncat_l、wcsncat、_wcsncat_l、_mbsncat、_mbsncat_lstrncat, _strncat_l, wcsncat, _wcsncat_l, _mbsncat, _mbsncat_l
strncmp、wcsncmp、_mbsncmp、_mbsncmp_lstrncmp, wcsncmp, _mbsncmp, _mbsncmp_l
strncpy、_strncpy_l、wcsncpy、_wcsncpy_l、_mbsncpy、_mbsncpy_lstrncpy, _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_lstrrchr, wcsrchr, _mbsrchr, _mbsrchr_l
strspn、wcsspn、_mbsspn、_mbsspn_lstrspn, wcsspn, _mbsspn, _mbsspn_l