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(包括终止空字符)复制到 strDestination 指定的位置。 如果源和目标字符串重叠,则 strcpy 的行为是未定义的。

重要

由于 strcpy 不会在复制 strSource 前检查 strDestination 中空间是否足够,所以这可能会造成缓冲区溢出。 因此,建议改用 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>

有关兼容性的详细信息,请参阅 兼容性

示例

// 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!

另请参阅

字符串操作
strcatwcscat_mbscat
strcmpwcscmp_mbscmp
strncat_strncat_lwcsncat_wcsncat_l_mbsncat_mbsncat_l
strncmpwcsncmp_mbsncmp_mbsncmp_l
strncpy_strncpy_lwcsncpy_wcsncpy_l_mbsncpy_mbsncpy_l
_strnicmp_wcsnicmp_mbsnicmp_strnicmp_l_wcsnicmp_l_mbsnicmp_l
strrchrwcsrchr_mbsrchr_mbsrchr_l
strspnwcsspn_mbsspn_mbsspn_l