多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
# C `strcpy()`函数 > 原文: [https://beginnersbook.com/2017/11/c-strcpy-function/](https://beginnersbook.com/2017/11/c-strcpy-function/) **`strcpy()`函数**将一个字符串复制到另一个字符串。 ## C `strcpy()`函数声明 ```c char *strcpy(char *str1, const char *str2) ``` `str1` - 这是复制其他字符串`str2`的值的目标字符串。函数的第一个参数。 `str2` - 这是源字符串,该字符串的值被复制到目标字符串。这是函数的第二个参数。 ## `strcpy()`的返回值 此函数返回指向目标字符串的指针,或者您可以说它返回目标字符串`str1`。 ## 示例:`strcpy()`函数 ```c #include <stdio.h> #include <string.h> int main () { char str1[20]; char str2[20]; //copying the string "Apple" to the str1 strcpy(str1, "Apple"); printf("String str1: %s\n", str1); //copying the string "Banana" to the str2 strcpy(str2, "Banana"); printf("String str2: %s\n", str2); //copying the value of str2 to the string str1 strcpy(str1, str2); printf("String str1: %s\n", str1); return 0; } ``` 输出: ```c String str1: Apple String str2: Banana String str1: Banana ``` 正如我在帖子的开头所提到的,这个函数返回指向目标字符串的指针,这意味着如果我们显示函数的返回值,它应该在将源字符串复制到目标字符串后显示目标字符串的值。让我们举一个例子来理解这一点。 ```c #include <stdio.h> #include <string.h> int main () { char str[20]; /* copying the string "Apple" to the str and * displaying the return value of strcpy() */ printf("Return value of function: %s\n", strcpy(str, "Apple")); printf("String str1: %s\n", str); return 0; } ``` 输出: ```c Return value of function: Apple String str1: Apple ``` #### 相关文章: 1. [C - `strcat()`示例](https://beginnersbook.com/2017/11/c-strcat-function-with-example/) 2. [C - `strchr()`示例](https://beginnersbook.com/2017/11/c-strchr-function/) 3. [C - `strcmp()`示例](https://beginnersbook.com/2017/11/c-strcmp-function/) 4. [C - `strcoll()`示例](https://beginnersbook.com/2017/11/c-strcoll-function/)