C-style string

Source: Internet
Author: User

A String constant is stored in the constant storage area (Text constant area), so its value cannot be changed.

# Include <iostream>

Using namespace STD;
Int main (void)
{
Char * P = "C ++ ";
* (P + 1) = 'D ';
Cout <* P <Endl;
Char ch [] = "C ++ ";
Ch [0] = 'D ';
Cout <ch <Endl;
Return 0;

}

AboveProgramA running error occurs. The pointer P points to the constant storage area. It is invalid to change it. P is more accurately defined as const char * P = "C ++ ";

The storage space of the CH array is allocated in the stack, so it can be changed.


When using strcat or other functions that change strings, note that the char * in the parameter is a const char *. If it is a const char *, a runtime error occurs.

# Include <iostream>
# Include <cstring>
Using namespace STD;
Int main (void)
{
Char * P = "andf ";
Strcat (P, "SF ");
Cout <p <Endl;
Return 0;
}

The above program is compiled, but an error occurs during running.

# Include <iostream>
# Include <cstring>
Using namespace STD;
Int main (void)
{
Char * P;
Strcat (P, "SF ");
Cout <p <Endl;
P [0] = '0 ';
Cout <p <Endl;
Return 0;
}

The above program can run correctly, and the string pointed to by P can be changed, proving that P does not point to the constant storage area.


The prerequisite for using the strn version is to calculate the size correctly.

# Include <iostream>
# Include <cstring>
Using namespace STD;
Int main (void)
{
Char * P1 = "ABCD", * P2 = "ad ";
Char P4 [1];
Strncpy (P4, P2, 1 );
Cout <P4 <Endl;
Return 0;
}



for strncpy: no null-character is implicitly appended to the end of destination ,
SO destination will only be null-terminated if the length of the C string in source is
less than num . we need to explicitly add null characters after the CPY operation.

# Include <iostream>
# Include <cstring>
Using namespace STD;
Int main (void)
{
Char * P1 = "ABCD", * P2 = "ad ";
Char P3 [5];
Strncpy (P3, P2, 2 );
Cout <P3 <Endl;
Return 0;
}

The above program gets the following results:


We can see from the results that we need to add P3 [2] = '\ 0' after the strncpy operation. The corresponding results are as follows:


When the char [] array is input, CIN> CH; is the same as the input string, starting from the first non-white space character and ending with the next white space character.


For the string type, you can use the c_str member function to return a C-style string, which returns a pointer to an array of the const char type. The array returned by c_str is not necessarily valid. Operations on string will change the returned value.

# Include <iostream>
# Include <string>
Using namespace STD;
Int main (void)
{
String STR ("ajkdfj ");
Const char * Ch = Str. c_str ();
Cout <ch <Endl;
STR = "ABCD ";
Cout <ch <Endl;
Return 0;
}

In the above program, the STR change also changes the value of the object to which ch points.

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.