函式
<cstring>

strcpy

char * strcpy ( char * destination, const char * source );
複製字串
source 指向的 C 字串複製到 destination 指向的陣列中,包括終止的空字元(並在該處停止)。

為避免溢位,destination 指向的陣列的大小應足夠容納與 source 相同的 C 字串(包括終止的空字元),並且不應與 source 在記憶體中重疊。

引數

destination
指向要複製內容的目標陣列的指標。
source
要複製的 C 字串。

返回值

返回 destination

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
/* strcpy example */
#include <stdio.h>
#include <string.h>

int main ()
{
  char str1[]="Sample string";
  char str2[40];
  char str3[40];
  strcpy (str2,str1);
  strcpy (str3,"copy successful");
  printf ("str1: %s\nstr2: %s\nstr3: %s\n",str1,str2,str3);
  return 0;
}

輸出

str1: Sample string
str2: Sample string
str3: copy successful


另見