共用方式為


strcpy、wcscpy、_mbscpy

複製字串。 更多這些函式的可用安全版本,請參閱 strcpy_s、wcscpy_s、_mbscpy_s

重要

_mbscpy 無法用於在 Windows 執行階段 中執行的應用程式。如需詳細資訊,請參閱/ZW 不支援 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不會在複製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>

如需其他相容性資訊,請參閱 相容性

範例

// 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 );
}
  

.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