目录html
零基础 C/C++ 学习路线推荐 : C/C++ 学习目录 >> C 语言基础入门python
C
语言在 string.h
中 strcpy
函数,可用完成 char 字符串拷贝;而今天即将介绍的 strcpy_s
函数其实和 strcpy
函数相似, strcpy
函数使用时,咱们也注意到了两个问题:编程
详细介绍请参考:C 语言 error C4996: This function or variable may be unsafewindows
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. 解决办法:include 以后添加代码 #pragma warning( disable : 4996)
strcpy_s
是系统的安全函数,微软在 2005 后建议用一系统所谓安全的函数,这中间就有 strcpy_s
取代了 strcpy
;
strcpy
函数没有方法来保证有效的缓冲区尺寸,因此它仅仅能假定缓冲足够大来容纳要拷贝的字符串。在程序执行时,这将致使不可预料的行为,容易致使程序崩溃,例如以下代码:安全
/******************************************************************************************/ //@Author:猿说编程 //@Blog(我的博客地址): www.codersrc.com //@File:C语言教程 - C语言 strcpy_s 函数 //@Time:2021/06/03 08:00 //@Motto:不积跬步无以致千里,不积小流无以成江海,程序人生的精彩须要坚持不懈地积累! /******************************************************************************************/ 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);
strcpy_s
函数能够经过设置目标缓冲区大小来够避免上面的不可预料的行为,语法以下:ide
/* *描述:此类函数是用于对字符串进行复制(拷贝)。 * *参数: * [out] strDestination:拷贝完成以后的字符串 * [in] numberOfElements: strDestination目标缓冲区长度 * [in] strSource:须要拷贝的字符串 * *返回值:返回一个整数,0表示复制成功,返回非0值表明复制不成功,不一样的值表示不一样的错误,具体内容能够查阅MSDN手册 */ errno_t strcpy_s(char *strDestination , size_t numberOfElements , const char *strSource);
/******************************************************************************************/ //@Author:猿说编程 //@Blog(我的博客地址): www.codersrc.com //@File:C语言教程 - C语言 strcpy_s 函数 //@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_s函数 - www.codersrc.com" }; char dst[1024] = { 0 }; int len_src = sizeof(src)/sizeof(char); int len_dst = sizeof(dst) / sizeof(char); printf("len_src:%d len_dst:%d\n", len_src,len_dst); printf("strcpy_s以前 dst:%s\n", dst); strcpy_s(dst, sizeof(dst)/sizeof(dst[0]), src); printf("strcpy_s以后 dst:%s\n", dst); printf("\n"); system("pause"); } /* 输出: len_src:1024 len_dst:1024 strcpy_s以前 dst: strcpy_s以后 dst:C/C++教程-strcpy_s函数 - www.codersrc.com 请按任意键继续. . . */
注意:strcpy_s
函数第二个参数,是设置目标缓冲区大小,并不是原始缓冲区大小函数
strcpy_s(dst, sizeof(dst)/sizeof(dst[0]), src); //正确写法 strcpy_s(dst, sizeof(src)/sizeof(src[0]), src); //错误写法
在 char
字符串中有做介绍,字符串默认都是'\0'
结尾,strcpy_s
函数在拷贝过程当中,若是遇到'\0'
结束符,那么直接结束拷贝,看下面例子:学习
/******************************************************************************************/ //@Author:猿说编程 //@Blog(我的博客地址): www.codersrc.com //@File:C语言教程 - C语言 strcpy_s 函数 //@Time:2021/06/03 08:00 //@Motto:不积跬步无以致千里,不积小流无以成江海,程序人生的精彩须要坚持不懈地积累! /******************************************************************************************/ char src[1024] = { "C/C++教程-strcpy_s函数\0 - www.codersrc.com" }; char dst[1024] = { 0 }; printf("strcpy_s以前 dst:%s\n", dst); strcpy_s(dst, sizeof(dst)/sizeof(dst[0]), src); printf("strcpy_s以后 dst:%s\n", dst); printf("\n"); system("pause"); /* 输出: strcpy_s以前 dst: strcpy_s以后 dst:C/C++教程-strcpy_s函数 请按任意键继续. . . */
重上面的输出结果能够看出:strcpy_s
函数在拷贝的时候,若是遇到 '\0'
,那么拷贝直接结束,因此上面使用 strcpy_s
拷贝的时候,dst
字符串明显少了一段字符" - www.codersrc.com"
;字体
未经容许不得转载:猿说编程 » C 语言 strcpy_s 函数插件
本文由博客 - 猿说编程 猿说编程 发布!