C语言 strcat 函数 - C语言零基础入门教程 男娘i 2022-09-04 14:53 154阅读 0赞 目录 * 一.strcat 函数简介 * 二.strcat 函数原理 * 三.strcat 函数实战 * 四.注意 strcat 函数崩溃问题 * 五.猜你喜欢 > 零基础 C/C++ 学习路线推荐 : [C/C++ 学习目录][C_C_] >> [C 语言基础入门][C] ## 一.strcat 函数简介 ## 前面文章中介绍了关于字符串拷贝的相关函数,例如:[`strcpy` 函数][strcpy_] / [`strcpy_s` 函数][strcpy_s_]/ [`memcpy` 函数][memcpy_] / [`memcpy_s` 函数][memcpy_s_]等等,今天我们将介绍一个新的 [`C` 语言][C]字符串处理函数 `strcat`,`strcat` 函数主要用于[字符串][Link 1]拼接,该函数语法如下: /* *描述:此类函数是用于对字符串进行拼接, 将两个字符串连接再一起 * *参数: * [in] strSource:需要追加的字符串 * [out] strDestination:目标字符串 * *返回值:指向拼接后的字符串的指针 */ //头文件:string.h char* strcat(char* strDestination, const char* strSource); **1.[`strcat` 函数][strcat_]把 `strSource` 所指向的字符串追加到 `strDestination` 所指向的字符串的结尾,所以必须要保证 `strDestination` 有足够的内存空间来容纳两个字符串,否则会导致溢出错误。** \*\* 2.\*\*`strDestination`**末尾的** `\0` **会被覆盖,**`strSource`**末尾的** `\0` **会一起被复制过去,最终的字符串只有一个 `\0`** **;** 3.如果使用 strcat 函数提示 error:4996,解决办法请参考:[error C4996: ‘fopen’: This function or variable may be unsafe][error C4996_ _fopen_ This function or variable may be unsafe] error C4996: 'strcat': This function or variable may be unsafe. Consider using strcat_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. ## 二.strcat 函数原理 ## > `strcat` 函数原理:`dst` 内存空间大小 = 目标字符串长度 + 原始字符串场地 + ‘\\0’; **获取内存空间大小使用 [`sizeof` 函数][sizeof_](获取内存空间大小)**;**获取字符串长度使用 [`strlen` 函数][strlen_](查字符串长度)** ## 三.strcat 函数实战 ## /******************************************************************************************/ //@Author:猿说编程 //@Blog(个人博客地址): www.codersrc.com //@File:C语言教程 - C语言 strcat 函数 //@Time:2021/06/06 08:00 //@Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累! /******************************************************************************************/ #include "stdafx.h" #include<stdlib.h> #include<stdio.h> #include<string.h> #include "windows.h" //error C4996: 'strcat': This function or variable may be unsafe. Consider using strcat_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++教程-strcat函数" }; char dst[1024] = { "www.codersrc.com"}; printf("strcat之前 dst:%s\n", dst); //空字符串 strcat(dst, src); printf("strcat之后 dst:%s\n", dst);// system("pause"); } /* 输出结果: strcat之前 dst:www.codersrc.com strcat之后 dst:www.codersrc.comC/C++教程-strcat函数 请按任意键继续. . . */ ## 四.注意 strcat 函数崩溃问题 ## char src[1024] = { "C/C++教程-strcat函数" }; char dst[5] = { "www"}; printf("strcat之前 dst:%s\n", dst); // strcat(dst, src); //dst只有5个字节,如果把src拼接到dst尾部,dst的空间并不能存放下src的所有字符,溢出崩溃 printf("strcat之后 dst:%s\n", dst); ## 五.猜你喜欢 ## 1. [安装 Visual Studio][Visual Studio] 2. [安装 Visual Studio 插件 Visual Assist][Visual Studio _ Visual Assist] 3. [Visual Studio 2008 卸载][Visual Studio 2008] 4. [Visual Studio 2003/2015 卸载][Visual Studio 2003_2015] 5. [设置 Visual Studio 字体/背景/行号][Visual Studio 1] 6. [C 语言格式控制符/占位符][C 1] 7. [C 语言逻辑运算符][C 2] 8. [C 语言三目运算符][C 3] 9. [C 语言逗号表达式][C 4] 10. [C 语言自加自减运算符(++i / i++)][C _i _ i] 11. [C 语言 for 循环][C _ for] 12. [C 语言 break 和 continue][C _ break _ continue] 13. [C 语言 while 循环][C _ while] 14. [C 语言 do while 和 while 循环][C _ do while _ while] 15. [C 语言 switch 语句][C _ switch] 16. [C 语言 goto 语句][C _ goto] 17. [C 语言 char 字符串][Link 1] 18. [C 语言 strlen 函数][strlen_] 19. [C 语言 sizeof 函数][sizeof_] 20. [C 语言 sizeof 和 strlen 函数区别][C _ sizeof _ strlen] 21. [C 语言 strcpy 函数][strcpy_] 22. [C 语言 strcpy\_s 函数][strcpy_s_] 23. [C 语言 strcpy 和 strcpy\_s 函数区别][C _ strcpy _ strcpy_s] 24. [C 语言 memcpy 和 memcpy\_s 区别][C _ memcpy _ memcpy_s] 25. [C 语言 strcat 函数][strcat_] 未经允许不得转载:[猿说编程][Link 2] » [C 语言 strcat 函数][strcat_] [C_C_]: https://www.codersrc.com/c-c [C]: https://www.codersrc.com/c%e8%af%ad%e8%a8%80%e5%9f%ba%e7%a1%80 [strcpy_]: https://www.codersrc.com/archives/7891.html [strcpy_s_]: https://www.codersrc.com/archives/7918.html [memcpy_]: https://www.codersrc.com/archives/8005.html [memcpy_s_]: https://www.codersrc.com/archives/8016.html [Link 1]: https://www.codersrc.com/archives/7815.html [strcat_]: https://www.codersrc.com/archives/8108.html [error C4996_ _fopen_ This function or variable may be unsafe]: https://www.codersrc.com/archives/8057.html [sizeof_]: https://www.codersrc.com/archives/7851.html [strlen_]: https://www.codersrc.com/archives/7834.html [Visual Studio]: https://www.codersrc.com/archives/7250.html [Visual Studio _ Visual Assist]: https://www.codersrc.com/archives/7280.html [Visual Studio 2008]: https://www.codersrc.com/archives/7288.html [Visual Studio 2003_2015]: https://www.codersrc.com/archives/7292.html [Visual Studio 1]: https://www.codersrc.com/archives/7284.html [C 1]: https://www.codersrc.com/archives/7460.html [C 2]: https://www.codersrc.com/archives/7548.html [C 3]: https://www.codersrc.com/archives/7558.html [C 4]: https://www.codersrc.com/archives/7577.html [C _i _ i]: https://www.codersrc.com/archives/7579.html [C _ for]: https://www.codersrc.com/archives/7581.html [C _ break _ continue]: https://www.codersrc.com/archives/7583.html [C _ while]: https://www.codersrc.com/archives/7585.html [C _ do while _ while]: https://www.codersrc.com/archives/7587.html [C _ switch]: https://www.codersrc.com/archives/7782.html [C _ goto]: https://www.codersrc.com/archives/7796.html [C _ sizeof _ strlen]: https://www.codersrc.com/archives/7865.html [C _ strcpy _ strcpy_s]: https://www.codersrc.com/archives/7945.html [C _ memcpy _ memcpy_s]: https://www.codersrc.com/archives/7973.html [Link 2]: https://www.codersrc.com
还没有评论,来说两句吧...