String problem, string

Source: Internet
Author: User

String problem, string

1. All strings in C/C ++ end with '\ 0. If you do not care about it, it will cause the string to cross-border, for example:

Char str [10];

Strcpy (str, "0123456789 ");

When copying a string, you must apply for one more byte of Memory Based on the string length to '\ 0'

 

2. check what the code will output:

Char str1 [] = "Hello ZCM ";
Char str2 [] = "Hello ZCM ";

Char * str3 = "Hello ZCM ";
Char * str4 = "Hello ZCM ";

If (str1 = str2)
{
Cout <"str1 is same as str2 \ n ";
}
Else
{
Cout <"str1 is not same as str2 \ n ";
}

If (str3 = str4)
{
Cout <"str3 is same as str4 \ n ";
}
Else
{
Cout <"str3 is not same as str4 \ n ";
}

Output:

C/C ++ puts the constant string in a separate memory zone, that is, the text constant zone. Other articles will summarize the code segments and properties of c ++,

I will not repeat it here. Str1 and str2 are two string arrays. We will allocate 10 bytes of space for them and copy "Hello ZCM. Institute

This is an array with two different initial addresses.

Str3 and str4 are two pointers. We don't need to allocate memory for it to store string content. We just need to point it to

The memory address is enough. Because "Hello ZCM" is a character constant and has only one copy in the memory, str3 and str4 point to the same address.

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.