Advance understanding of software development (10) String handling functions and exception protection

Source: Internet
Author: User
Tags date printf

In software development projects, there are often programs to manipulate strings. To do this, the C function library provides some functions for handling strings, which is very convenient to use. But because the string has the length, if arbitrarily to the different string connection and the copy and so on operation, may have the unexpected consequence.

Therefore, in the actual development process, it is very important to protect the string processing functions. This paper describes how to correctly use the string processing function for C programming.

1. Strcat and STRNCAT functions

The role of the STRCAT function is to concatenate a string from a two-character array. In MSDN, it is defined as:

Char *strcat (char *strdestination, const char *strsource);

Remarks:the strcat function appends strsource to strdestination and terminates the resulting string with a null character . The initial character of strsource overwrites the terminating null character of strdestination. It returns the destination string (strdestination).

The Strcat function strsource strings to the strdestination, and the final return value is the string strdestination after the completion of the assembly.

Here is a problem, if the length of the string strsource is greater than the length of the strdestination array, there will be an array of errors in the bounds, the program will crash. As shown in the following code:

/*************************************************************** * All rights reserved (C) 2014, Zhou Zhaoxiong.
    
* * File name: strcattest.c * Content Summary: For testing strcat function * Other Description: No * current version: V1.0 * Author: Zhou * Completion Date: 20140405 * * Modify the Record 1://Modify history, including modified date, version number, modifier and modified content, etc. * Modified Date: * Version number: * Modified by: * Modified content: **************
    
     
    
/#include <stdio.h> #include <string.h>                        typedef signed Char INT8;                       Redefine data type typedef signed INT INT32; Redefining data Types/********************************************************************** * Function Description: main function * lose In parameter: no * output parameter: no * return value: No * other Description: No * Modified Date version number modify the content *----------------           --------------------------------------------------------------------------* 20140405 V1.0 Zhou Create ***********************/INT32 Main (void) {INT8 szstrdestination[10] = "he
    
       Llo ";
    
          
    
       INT8 szstrsource[10] = "Hello123";
    
       First print the source and destination strings printf ("The source string is:%s\n", Szstrsource);
    
          
    
      printf ("The destination string is:%s\n", szstrdestination);     strcat (Szstrdestination, Szstrsource); Call strcat function//print the string printf after the completion of the assembly ("the changed destination string is:%s\n", szst
    
     
    
       Rdestination);
    
return 0; }

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.