C language strcat_s function - C language zero foundation tutorial

catalogue

Zero foundation C/C + + learning route recommendation: C/C + + Learning directory >> Introduction to basic C language

1, 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;

2, 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 the size of memory space); Get string length usage strlen function (check string length)

3, strcat_s function practice

/******************************************************************************************/
//@Author: ape programming
//@Blog (personal blog address): www.codersrc.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;

4, Guess you like it

  1. Install Visual Studio
  2. Install the Visual Studio plug-in Visual Assist
  3. Visual Studio 2008 uninstall
  4. Visual Studio 2003/2015 uninstall
  5. Set Visual Studio font / background / line number
  6. C language format controller / placeholder
  7. C language logical operators
  8. C language ternary operator
  9. C language comma expression
  10. C language self addition and self subtraction operator (+ + i / i + +)
  11. C language for loop
  12. C language break and continue
  13. C language while loop
  14. C language do while and while loop
  15. C language switch statement
  16. C language goto statement
  17. C language char string
  18. C language strlen function
  19. sizeof function in C language
  20. Differences between sizeof and strlen functions in C language
  21. C language strcpy function
  22. C language strcpy_s function
  23. C language strcpy and strcpy_s function difference
  24. C language memcpy and memcpy_s difference
  25. C language strcat function
  26. C language strcat_s function

No reprint without permission: Ape programming ยป C language strcat_s function

Keywords: C

Added by ruben- on Sat, 30 Oct 2021 13:32:09 +0300