目录
一.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 函数提示 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(个人博客地址): //@File:C语言教程 - C语言 strcpy 函数//@Time:/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函数 - " };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函数 - 请按任意键继续. . .*/
2.strcpy 函数拷贝内容以’\0’结尾
在char
字符串中有作介绍,字符串默认都是'\0'
结尾,strcpy
函数在拷贝过程中,如果遇到'\0'
结束符,那么直接结束拷贝,看下面例子:
/******************************************************************************************///@Author:猿说编程//@Blog(个人博客地址): //@File:C语言教程 - C语言 strcpy 函数//@Time:/06/03 08:00//@Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!/******************************************************************************************/char src[1024] = { "C/C++教程-strcpy函数\0 - " };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
字符串明显少了一段字符" - "
;
3.strcpy 函数注意崩溃问题
如果使用strcpy
的时候strDestination
内存大小比strSource
内存大小更小,程序运行会崩溃,strcpy
函数在字符串拷贝的时候并不会检查两个字符串大小,举个例子:
/******************************************************************************************///@Author:猿说编程//@Blog(个人博客地址): //@File:C语言教程 - C语言 strcpy 函数//@Time:/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 - " };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 卸载Visual Studio / 卸载设置 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 函数