C 语言 strcpy() 函数用于将一个字符串复制到另一个字符串中,包括结尾的空字符。
原型
char *strcpy(char* destination, const char* source);它把 source 指向的字符串(含结尾 \0)复制到 destination 指向的数组,并返回 destination 指针。
参数
destination- 目标数组的指针;source- 要被复制的源字符串。
返回值
返回 destination 的指针。
示例
#include <stdio.h>
#include <string.h>
int main()
{
char str1[] = "Hello World!";
char str2[40];
strcpy(str2, str1);
printf("str2 = %s\n", str2);
return 0;
}输出:
str2 = Hello World!注意事项
destination 必须足够大以容纳 source(含 \0),否则缓冲区溢出。要求更安全的限定长度复制时用 strncpy。