strcpy ()- 复制字符串

格式

#include <string.h>
char *strcpy(char *string1, const char *string2);

语言级别

ANSI

线程安全

描述

strcpy() 函数将 string2(包括结尾空字符) 复制到 string1指定的位置。

strcpy() 函数对以 null 结束的字符串进行操作。 函数的字符串自变量应包含标记字符串结束的空字符 (\0)。 不执行长度检查。 不应将文字字符串用于 string1 值,尽管 string2 可能是文字字符串。

返回值

strcpy() 函数返回指向复制的字符串 (string1) 的指针。

示例

此示例将 source 的内容复制到 destination
#include <stdio.h>
#include <string.h>
 
#define SIZE 40
 
int main(void)
{
  char source[SIZE] = "This is the source string";
  char destination[SIZE] = "And this is the destination string";
  char * return_string;
 
  printf( "destination is originally = \"%s\"\n", destination );
  return_string = strcpy( destination, source );
  printf( "After strcpy, destination becomes \"%s\"\n", destination );
}
 
/*****************  Output should be similar to:  *****************
 
destination is originally = "And this is the destination string"
After strcpy, destination becomes "This is the source string"
*/

相关信息