C language string

Source: Internet
Author: User

1. strlen (char * Str) returns the number of characters except the ending character/0 in Str.
2. The system will open up a piece of memory in the string area for all strings "", both global and local. For example:
Char * P = "hello ";
The system opens a memory for hello and Points P to the memory.
3. The = cannot be used to initialize the dynamically allocated string. strcpy should be used. For example, append world to the end of Hello:

// The correct method
Char * P = (char *) malloc (11 );
Char * STR = "world ";
Strcpy (P, "hello");/* assign a value like this */
Strcat (p, STR );

 

// Incorrect method
Char * P = (char *) malloc (11 );
Char * STR = "world ";
P = "hello";/* only change the storage area (from the heap area to the String constant area) referred to by P, but the newly allocated string is not initialized */
Strcat (p, STR );
5. strcat (char * str1, char * str2) does not dynamically increase the length of str1, when calling the strcat function, ensure that the target string has enough length to accommodate the entire string connected by the target string and the source string. In addition, str1 cannot be a null string,

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.