零基础 C/C++ 学习路线推荐 : C/C++ 学习目录 >> C 语言基础入门
一.memcpy函数/strcpy函数简介
C
语言在 string.h
中 strcpy
函数和 memcpy
函数,都可以完成 char 字符串拷贝,注意:
1.strcpy
函数和 strcpy_s
函数在拷贝过程中,如果遇到'\0'
结束符,那么直接结束拷贝;memcpy
函数拷贝过程中就算遇到'\0'
结束符也不会结束;
2.如果使用 memcpy
或者 strcpy
函数都会提示 error:4996
,编译器更加推荐使用 strcpy_s
函数或者 memcpy_s
函数,否则也可以参考:error C4996: ‘fopen’: This function or variable may be unsafe
<code>error C4996: 'memcpy': This function or variable may be unsafe. </code><code>Consider using memcpy_s instead. To disable deprecation, </code><code>use _CRT_SECURE_NO_WARNINGS. See online help for details. </code><code>error C4996: 'memcpy': This function or variable may be unsafe. </code><code>Consider using memcpy_s instead. To disable deprecation, </code><code>use _CRT_SECURE_NO_WARNINGS. See online help for details. </code>
error C4996: 'memcpy': This function or variable may be unsafe.error C4996: 'memcpy': This function or variable may be unsafe.error C4996: 'memcpy': This function or variable may be unsafe.
Consider using memcpy_s instead. To disable deprecation,Consider using memcpy_s instead. To disable deprecation,Consider using memcpy_s instead. To disable deprecation,
use _CRT_SECURE_NO_WARNINGS. See online help for details.use _CRT_SECURE_NO_WARNINGS. See online help for details.use _CRT_SECURE_NO_WARNINGS. See online help for details.
3.不管是 memcpy
函数或者 strcpy
函数 必须保证 dst
空间足够大,能够容纳 src
,如果 dst
内存空间大小比 src
更小,会导致溢出错误,引起程序崩溃!可以通过 sizeof
函数查看内存内存大小,举个例子:50ml
的水杯能倒进 500ml
的水杯没问题,500ml
的水杯倒进 50ml
的水杯,会溢出很多水;
二.memcpy函数/strcpy函数实战
1.strcpy函数属于字符串拷贝
在 char
字符串中有作介绍,字符串默认都是'\0'
结尾,strcpy
函数或者 strcpy_s
函数在拷贝过程中,如果遇到'\0'
结束符,那么直接结束拷贝,看下面例子:
/******************************************************************************************///@Author:猿说编程//@Blog(个人博客地址): www.codersrc.com//@File:C语言教程 - C语言 memcpy 和 strcpy 函数区别//@Time:2021/06/04 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[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函数请按任意键继续. . .*//******************************************************************************************/ //@Author:猿说编程 //@Blog(个人博客地址): www.codersrc.com //@File:C语言教程 - C语言 memcpy 和 strcpy 函数区别 //@Time:2021/06/04 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[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函数 请按任意键继续. . . *//******************************************************************************************/ //@Author:猿说编程 //@Blog(个人博客地址): www.codersrc.com //@File:C语言教程 - C语言 memcpy 和 strcpy 函数区别 //@Time:2021/06/04 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[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"
;
2.memcpy函数属于内存拷贝
而 memcpy 函数不同,memcpy 属于内存拷贝,即便在拷贝过程中遇到'\0'
结束符,也不会结束拷贝,举个例子:
/******************************************************************************************///@Author:猿说编程//@Blog(个人博客地址): www.codersrc.com//@File:C语言教程 - C语言 memcpy 和 strcpy 函数区别//@Time:2021/06/04 08:00//@Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!/******************************************************************************************/char src[1024] = { "C/C++教程-memcpy函数\0 - www.codersrc.com" };char dst[1024] = { 0 };printf("memcpy之前 dst:%s\n", dst);memcpy(dst, src);printf("memcpy之后 dst:%s\n", dst);printf("\n");system("pause");/*输出:memcpy之前 dst:memcpy之后 dst:C/C++教程-memcpy函数\0 - www.codersrc.com请按任意键继续. . .*//******************************************************************************************/ //@Author:猿说编程 //@Blog(个人博客地址): www.codersrc.com //@File:C语言教程 - C语言 memcpy 和 strcpy 函数区别 //@Time:2021/06/04 08:00 //@Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累! /******************************************************************************************/ char src[1024] = { "C/C++教程-memcpy函数\0 - www.codersrc.com" }; char dst[1024] = { 0 }; printf("memcpy之前 dst:%s\n", dst); memcpy(dst, src); printf("memcpy之后 dst:%s\n", dst); printf("\n"); system("pause"); /* 输出: memcpy之前 dst: memcpy之后 dst:C/C++教程-memcpy函数\0 - www.codersrc.com 请按任意键继续. . . *//******************************************************************************************/ //@Author:猿说编程 //@Blog(个人博客地址): www.codersrc.com //@File:C语言教程 - C语言 memcpy 和 strcpy 函数区别 //@Time:2021/06/04 08:00 //@Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累! /******************************************************************************************/ char src[1024] = { "C/C++教程-memcpy函数\0 - www.codersrc.com" }; char dst[1024] = { 0 }; printf("memcpy之前 dst:%s\n", dst); memcpy(dst, src); printf("memcpy之后 dst:%s\n", dst); printf("\n"); system("pause"); /* 输出: memcpy之前 dst: memcpy之后 dst:C/C++教程-memcpy函数\0 - www.codersrc.com 请按任意键继续. . . */
很明显,memcpy
函数内存拷贝的时候,'\0'
仅仅是当作了内存中的数据,并不代表拷贝结尾;
三.猜你喜欢
- 安装 Visual Studio
- 安装 Visual Studio 插件 Visual Assist
- Visual Studio 2008 卸载
- Visual Studio 2003/2015 卸载
- C语言格式控制符/占位符
- C语言逻辑运算符
- C语言三目运算符
- C语言逗号表达式
- C语言自加自减运算符(++i / i++)
- C语言 do while 和 while 循环
- C语言 switch 语句
- C语言 goto 语句
- C语言 char 字符串
- C语言 strlen 函数
- C语言 sizeof 函数
- C语言 sizeof 和 strlen 函数区别
- C语言 strcpy 函数
- C语言 strcpy_s 函数
- C语言 strcpy 和 strcpy_s 函数区别
- C语言 memcpy 和 strcpy 函数区别
ChatGPT 3.5 国内中文镜像站免费使用啦
暂无评论内容