catalogue
- I strcat_ Introduction to s function
- II strcat_s-function principle
- III strcat_s function practice
- IV Guess you like it
Zero foundation C/C + + learning route recommendation: C/C + + Learning directory >> Introduction to basic C language
I strcat_ Introduction to s function
The previous article introduced the string splicing function strcat, and strcat_s function and strcat function Same, mainly used for character string Splicing;
strcat_s is the security function of the system. Microsoft suggested to use a so-called security function of the system after 2005, including strcat_s replaces strcat. The original strcat function has no way to ensure the effective buffer size, so it can only assume that the buffer is large enough to accommodate the string to be copied, which is easy to cause program crash. And strcat_s function can avoid this problem, strcat_s function The syntax is as follows:
/* *Description: this kind of function is used to splice strings and connect two strings together * *Parameters: * [in] strSource: String to append * [in] numberOfElements: String size after splicing (not the target string size nor the original string size) * [out] strDestination: Target string * *Return value: errno_t is a new type defined by Microsoft. This type is an integer, * Represents the error code. If it is 0, it means there is no error. If it is other values, a value will be thrown; */ //Header file: string h errno_t strcat_s(char *strDestination , size_t numberOfElements , const char *strSource );
1.strcat_ The s function appends the string pointed to by strSource to the end of the string pointed to by strDestination. Therefore, it must be ensured that strDestination has enough memory space to accommodate strSource and strDestination strings, otherwise overflow errors will be caused.
strcat_s function principle: dst memory space size = target string length + original string site + '\ 0';
2. The \ 0 at the end of strdestination will be overwritten, and the \ 0 at the end of strSource will be copied together. The final string has only one \ 0;
II strcat_s-function principle
strcat_s function principle: dst memory space size = target string length + original string site + '\ 0';
Get memory space usage sizeof function (get memory space size); get string length using strlen function (check string length)
III strcat_s function practice
/******************************************************************************************/ //@Author: ape programming //@Blog (personal blog address): www.codersrc.com com //@File:C language tutorial - C language strcat_s function //@Time:2021/06/05 08:00 //@Motto: no small steps can lead to thousands of miles. No small streams can lead to rivers and seas. The highlights of program life need to be accumulated unremittingly! /******************************************************************************************/ #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++course-strcat_s function" }; char dst[1024] = { "www.codersrc.com" }; //Note: strcat_s is the calculation of the second parameter, which is the string size after splicing, not the original string size or the target string size int len = strlen(src) + strlen(dst) + 1; printf("strcat_s before dst:%s\n", dst); // strcat_s(dst, len, src); printf("strcat_s after dst:%s\n", dst);// system("pause"); } Output results: strcat_s before dst:www.codersrc.com strcat_s after dst:www.codersrc.comC/C++course-strcat_s function Please press any key to continue. . .
be careful: strcat_s The second parameter is the string size after splicing, not the original string size or the target string size;
IV Guess you like it
- Install Visual Studio
- Install the Visual Studio plug-in Visual Assist
- Visual Studio 2008 uninstall
- Visual Studio 2003/2015 uninstall
- Set Visual Studio font / background / line number
- C language format controller / placeholder
- C language logical operators
- C language ternary operator
- C language comma expression
- C language self addition and self subtraction operator (+ + i / i + +)
- C language for loop
- C language break and continue
- C language while loop
- C language do while and while loop
- C language switch statement
- C language goto statement
- C language char string
- C language strlen function
- sizeof function in C language
- Differences between sizeof and strlen functions in C language
- C language strcpy function
- C language strcpy_s function
- C language strcpy and strcpy_s function difference
- C language memcpy and memcpy_s difference
- C language strcat function
- C language strcat_s function
No reprint without permission: Ape programming ยป C language strcat_s function