C + +--string

Source: Internet
Author: User

Tagged with: C + + size RET Initialize Hello and char C99 turn

1. Each string is terminated with '/' at the end, so that we can easily find the end of the string.

1 Char str[]; 2 strcpy (str,"0123456789");   // Error: Array is out of bounds. The actual length of "0123456789" is 11 bytes

Note: Remember the null character at the end of the string to prevent the array from overstepping.

2. C + + places a constant string in a separate memory area

In order to save memory, C + + places a constant string in a separate memory area. When several pointers are assigned to the same constant string, they actually point to the same memory address . But initializing arrays with constant memory is a different situation.

1 intMainintargcChar*argv[])2 {3     Charstr1[]="Hello World";4     Charstr2[]="Hello World";5 6     Char*str3="Hello World";7     Char*str4="Hello World";8 9     if(str1==str2)Tencout<<"str1 and str2 are same\n"; One     Else Acout<<"str1 and str2 is not same\n"; -  -     if(str3==STR4) thecout<<"STR3 and STR4 are same\m"; -     Else -cout<<"STR3 and STR4 is not same\n"; -  +     return 0; - } + //Output: A //str1 and str2 is not same at //STR3 and STR4 are same

C + +--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.