网站开发个人感想,夸克作文网站,查看网站空间大小,哈尔滨网站建立公司目录
一.strcpy 函数简介二.strcpy 函数实战 1.strcpy 函数简单使用2.strcpy 函数拷贝内容以’\0’结尾3.strcpy 函数注意崩溃问题 三.猜你喜欢 零基础 C/C 学习路线推荐 : C/C 学习目录 C 语言基础入门 一.strcpy 函数简介
C 语言在 string.h 中strcpy函数,可用完成…目录
一.strcpy 函数简介二.strcpy 函数实战 1.strcpy 函数简单使用2.strcpy 函数拷贝内容以’\0’结尾3.strcpy 函数注意崩溃问题 三.猜你喜欢 零基础 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 函数提示 error4996请参考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
#includestdlib.h
#includestdio.h
#includestring.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
#includestdlib.h
#includestdio.h
#includestring.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); // 1024int len_dst sizeof(dst) / sizeof(char); //10printf(len_src:%d len_dst:%d\n, len_src,len_dst);printf(strcpy之前 dst:%s\n, dst);strcpy(dst, src); // 很明显 dst 的空间大小并不能完全存放 srcprintf(strcpy之后 dst:%s\n, dst);
}
/*
输出
len_src:1024 len_dst:10
*/三.猜你喜欢
安装 Visual Studio安装 Visual Studio 插件 Visual AssistVisual Studio 2008 卸载Visual Studio 2003/2015 卸载设置 Visual Studio 字体/背景/行号C 语言格式控制符/占位符C 语言逻辑运算符C 语言三目运算符C 语言逗号表达式C 语言自加自减运算符(i / i)C 语言 for 循环C 语言 break 和 continueC 语言 while 循环C 语言 do while 和 while 循环C 语言 switch 语句C 语言 goto 语句C 语言 char 字符串C 语言 strlen 函数C 语言 sizeof 函数C 语言 sizeof 和 strlen 函数区别C 语言 strcpy 函数
未经允许不得转载猿说编程 » C 语言 strcpy 函数