This is the 15th day of my participation in the August More Text Challenge. For details, see:August is more challenging

directory

  • Introduction to strcat_s function
  • Strcat_s function principle
  • 3. Strcat_s function combat
  • Four. Guess you like it

Zero basic C/C++ learning route recommendation: C/C++ learning directory >> C language basic introduction

Introduction to strcat_s function

The previous article introduced the string concatenation function strcat, and the strcat_s function, like strcat, is primarily used for concatenation.

Strcat_s is a system security function. Microsoft proposed a system security function in 2005, which replaced strcat with strcat_s. There was no way to ensure a valid buffer size. So it can only assume that the buffer is large enough to hold the string to be copied, prone to crash. The strcat_s function is a good way to avoid this problem. The strcat_s function syntax is as follows:

Parameter: * [in] strSource: string to append * [in] numberOfElements: string to append * [in] numberOfElements: string to append String size after concatenation (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 in which * indicates an error code. If the value is 0, there is no error. H errno_t strcat_s(char *strDestination, size_t numberOfElements, const char *strSource);Copy the code

1.strcat_sfunctionstrSourceIs appended tostrDestinationTo the end of the string to which thestrDestinationThere is enough memory space to holdstrSourcestrDestinationTwo strings, otherwise it will cause an overflow error.

strcat_sFunction principle:dstMemory size = target string length + original string field + ‘\0’;

2.strDestinationAt the end of the\ 0Will be covered,strSourceAt the end of the\ 0Are copied together, and the final string is only one\ 0;

Strcat_s function principle

strcat_sFunction principle:dstMemory size = target string length + original string field + ‘\0’;

To get the sizeof the memory space, use sizeof. Get the length of the string using the strlen function

3. Strcat_s function combat

/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / / / @ Author: programming ape said // @blog (personal Blog address): www.codersrc.com // @file :C language tutorial - C language strcat_s function // @time :2021/06/05 08:00 08:00 //@Motto: a Motto is a Motto. The wonderful program life needs to accumulate 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++ -strcat_s "}; char dst[1024] = { "www.codersrc.com" }; Int len = strlen(SRC) + strlen(DST) + 1; int len = strlen(SRC) + strlen(DST); Printf ("strcat_s before DST :%s\n", DST); // strcat_s(dst, len, src); Printf ("strcat_s after DST :%s\n", DST); // system("pause"); } Output: strcat_s before DST :www.codersrc.com strcat_s after DST :www.codersrc.comC/C++ tutorial -strcat_s function please press any key to continue..Copy the code

Note:strcat_sThe calculation of the second argument, which is the concatenated string size, not the original string size or the target string size;

Four. 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 format control/placeholder
  7. C logical operators
  8. C ternary operator
  9. C language comma expression
  10. C ++ I/I ++
  11. C for loop
  12. C language break and continue
  13. C while loop
  14. C do while and while loops
  15. C switch statement
  16. C goto statement
  17. C char string
  18. C strlen function
  19. C sizeof function
  20. Sizeof and strlen are different in C
  21. C strcpy function
  22. C strcpy_s function
  23. The difference between strcpy and strcpy_s in C language
  24. C language memcpy and memcpy_s difference
  25. C strcat function
  26. C strcat_s function

The strcat_s function of the C language

This article is posted by the blog – Ape says Programming Ape says Programming!