目录

零基础 C/C++ 学习路线推荐 : ​​C/C++ 学习目录​​ >> ​​C 语言基础入门​

一.strcpy 函数简介

​C 语言​​在 ​​string.h​​ 中​​strcpy函数​​,可用完成 ​​char 字符串​​拷贝,语法如下:

/*
*描述:此类函数是用于对字符串进行复制(拷贝)。
*
*参数:
* [in] strSource:需要拷贝的字符串
* [out] strDestination:拷贝完成之后的字符串
*
*返回值:指向 strDestination 这个字符串的指针
*/
char* strcpy(char* strDestination, const char* strSource);


注意:

1.strcpy 函数在拷贝过程中,如果遇到​'\0'​结束符,那么直接结束拷贝

2.如果使用 strcpy 函数提示 error:4996,请参考:​​error C4996: ‘fopen’: This function or variable may be unsafe​

error C4996: 'strcpy': This function or variable may be unsafe.
Consider using strcpy_s instead. To disable deprecation,
use _CRT_SECURE_NO_WARNINGS. See online help for details.


3.必须保证 ​​strDestination​​ 空间足够大,能够容纳 ​​strSource​​,如果 ​​strDestination​​ 内存空间大小比 ​​strSource​​ 更小,会导致溢出错误,引起程序崩溃!可以通过 ​​sizeof 函数​​查看内存内存大小,举个例子:​50ml 的水杯能倒进​500ml​的水杯没问题,​500ml​ 的水杯倒进 ​50ml​ 的水杯,会溢出很多水;

二.strcpy 函数实战

1.strcpy 函数简单使用

/******************************************************************************************/
//@Author:猿说编程
//@Blog(个人博客地址): www.codersrc.com
//@File:C语言教程 - C语言 strcpy 函数
//@Time:2021/06/03 08:00
//@Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!
/******************************************************************************************/
#include "stdafx.h"
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include "windows.h"
//error C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
#pragma warning( disable : 4996)
void main()
{
char src[1024] = { "C/C++教程-strcpy函数 - www.codersrc.com" };
char dst[1024] = { 0 };
printf("strcpy之前 dst:%s\n", dst); //空字符串
strcpy(dst, src);
printf("strcpy之后 dst:%s\n", dst);//
printf("\n");
system("pause");
}
/*
输出:
strcpy之前 dst:
strcpy之后 dst:C/C++教程-strcpy函数 - www.codersrc.com
请按任意键继续. . .
*/


2.strcpy 函数拷贝内容以’\0’结尾

​​char​​ 字符串中有作介绍,字符串默认都是 ​'\0'​ 结尾,​strcpy​ 函数在拷贝过程中,如果遇到​'\0'​ 结束符,那么直接结束拷贝,看下面例子:

/******************************************************************************************/
//@Author:猿说编程
//@Blog(个人博客地址): www.codersrc.com
//@File:C语言教程 - C语言 strcpy 函数
//@Time:2021/06/03 08:00
//@Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!
/******************************************************************************************/

char src[1024] = { "C/C++教程-strcpy函数\0 - www.codersrc.com" };
char dst[1024] = { 0 };
printf("strcpy之前 dst:%s\n", dst);
strcpy(dst, src);
printf("strcpy之后 dst:%s\n", dst);
printf("\n");
system("pause");
/*
输出:
strcpy之前 dst:
strcpy之后 dst:C/C++教程-strcpy函数
请按任意键继续. . .
*/


重上面的输出结果可以看出:​​strcpy 函数​​在拷贝的时候,如果遇到 ​​'\0'​​,那么拷贝直接结束,所以上面使用 ​​strcpy​​ 拷贝的时候,​​dst​​ 字符串明显少了一段字符​​" - www.codersrc.com"​​;

3.strcpy 函数注意崩溃问题

如果使用 strcpy 的时候 ​strDestination​ 内存大小比 ​strSource​ 内存大小更小,程序运行会崩溃,​​strcpy​​ 函数​在字符串拷贝的时候并不会检查两个字符串大小,举个例子:

/******************************************************************************************/
//@Author:猿说编程
//@Blog(个人博客地址): www.codersrc.com
//@File:C语言教程 - C语言 strcpy 函数
//@Time:2021/06/03 08:00
//@Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!
/******************************************************************************************/


#include "stdafx.h"
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include "windows.h"
//error C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
#pragma warning( disable : 4996)
void main()
{
char src[1024] = { "C/C++教程-strcpy函数\0 - www.codersrc.com" };
char dst[10] = { 0 };
int len_src = sizeof(src)/sizeof(char); // 1024
int len_dst = sizeof(dst) / sizeof(char); //10
printf("len_src:%d len_dst:%d\n", len_src,len_dst);
printf("strcpy之前 dst:%s\n", dst);
strcpy(dst, src); // 很明显 dst 的空间大小并不能完全存放 src
printf("strcpy之后 dst:%s\n", dst);
}
/*
输出:
len_src:1024 len_dst:10
*/


三.猜你喜欢

  1. ​安装 Visual Studio​
  2. ​安装 Visual Studio 插件 Visual Assist​
  3. ​Visual Studio 2008 卸载​
  4. ​Visual Studio 2003/2015 卸载​
  5. ​设置 Visual Studio 字体/背景/行号​
  6. ​C 语言格式控制符/占位符​
  7. ​C 语言逻辑运算符​
  8. ​C 语言三目运算符​
  9. ​C 语言逗号表达式​
  10. ​C 语言自加自减运算符(++i / i++)​
  11. ​C 语言 for 循环​
  12. ​C 语言 break 和 continue​
  13. ​C 语言 while 循环​
  14. ​C 语言 do while 和 while 循环​
  15. ​C 语言 switch 语句​
  16. ​C 语言 goto 语句​
  17. ​C 语言 char 字符串​
  18. ​C 语言 strlen 函数​
  19. ​C 语言 sizeof 函数​
  20. ​C 语言 sizeof 和 strlen 函数区别​
  21. ​C 语言 strcpy 函数​