Modify string constants in C/C ++

Source: Internet
Author: User
Run the following code in (and only in tc2.0). Do not check the result first. Think about what you will get:
# Include <stdio. h>
# Include <stdlib. h>

Int
Main (INT argn, char * argv [])
{
Char * szstringa = "Hello, world! ";
Char * szstringb = "Hello, world! ";

* Szstringa = '-';
Puts (szstringb );

Return 0;
}
The output result is: "-ello, world! ";. Is it different from what you think?
Ansi c clearly states that the effect of modifying a String constant is undefined.
First, we need to know how to obtain the String constant? There is only one way: char * szstring = "Hello, world !"; This declaration produces a String constant. Char szstring [] = "Hello, world !"; Yes? No! The result is a string variable. Now, because ansi c does not require the compiler implementer to process string constants, Some compilers will regard the same multiple string constants as one (note: this optimization is only possible in string constants. Do not use generic constants of other types. For example: int num1 = 11; int num2 = 11; although it is two identical constants, modifying num1 does not affect num2). The purpose is to save memory space, when the string a is modified, B is also modified. It can be seen from this that tc2.0 has optimized the string constants, while other compilers (such as devc ++ 5.0) may not be optimized, and the result may not be compiled, or through compilation, but the result is a garbage value. Due to these uncertainties, we should avoid modifying string constants as much as possible.
What if you have to modify the string? Yes! Yes! Note: We want to modify the character. Therefore, you only need to define string variables instead of string constants. As mentioned above, char szstring [] = "Hello, world!" can be defined as an array !";; In this way, you can modify the string in the program.
Try the following code:
# Include <stdio. h>
# Include <stdlib. h>

Int
Main (INT argn, char * argv [])
{
Char szstringa [] = "Hello, world! ";
Char szstringb [] = "Hello, world! ";

Szstringa [0] = '-';
Puts (szstringb );

Return 0;
}
No problem.
OK. Remember: to modify the string, use char szstring [] = "Hello, world !"; Proceed!

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.