Single-Chip Microcomputer-C char array and string assignment, copying, additional considerations

Source: Internet
Author: User

Note: originality is personal experience, and I am not easy to learn. If there is anything wrong, I hope that experts can correct it!

 

Today, I am working on a small single-chip microcomputer program. I thought it was very simple to change the example program, and the results took a whole day. Many strcat functions are used in the program to copy strings. To enhance the universality of the program, I replaced the original string with a char array. As a result, many unexpected problems occur, such as chaotic output characters and system restart. After analysis, we found that the strcat function was playing a strange role. The C Program specifies that the string array uses \ 0 as the Terminator. That is to say, the strcat function is stopped only when it receives the Terminator. If it does not stop, it is not surprising that it is a good program.

Strlen, strcat, strcpy, and other functions are identified by the \ 0 or 0x00 Terminator. In practice, the string cannot be followed by a \ 0 character every time, which is not practical in some scenarios where the conversion type is required.

To get rid of this restriction, you can use strncpy and strncat functions to operate. The copy and additional length can be determined based on len. Use the strncpy function as an Example

Char * strncpy (char *Dst,/* Destination string */

Char *Src,/* Source string */

IntLen);/* Max characters to copy */)

Statement of The original article:StrncpyFunction copies at mostLenCharacters fromSrcToDst. Characters are copied until a null character ('\ 0') is copied orLenCharacters
Have been copied. If the lengthSrcIs lessLenThe remaining bytes in
DstAre padded with null characters ('\ 0 ').

Note: * pStrInf = strstr (buf, "="); // The starting address of the returned pointer is "=", that is, containing "= ".

 

 

 

The difference between strcat and strcpy is a copy function. Strcat is appended to the source string, and the strcpy function is copied from the 0 address of the source string !!

In order not to make mistakes in the future, record the errors and hope to help others.

 

 

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.