C_str ()

Source: Internet
Author: User

The prototype of the 1.string class member function c_str ():

Const Char *c_str ()const; // returns a null-terminated C string

The 2.c_str () function returns a pointer to a regular C string that is the same as the object of the string class, and is able to convert a string object to the style of a string in C by using the C_STR () function of the string class;  

The C_STR () function returns a pointer to the normal C string with the same contents as this string string.

This is to be compatible with the C language, there is no string type in C, so the string object must be converted to a string style in C by using the member function C_str () of the String class object.  

Note: Be sure to use the strcpy () function and so on to manipulate the pointer returned by the method C_str ()

    • For example: It's best not to:  
Char*string s="1234"= S.c_str//  c The last point of the content is garbage, because S object is destructor, its content is processed
    • This should be used:
Char c[]; string s="1234";  strcpy(c,s.c_str());

So that there is no error, C_STR () returns a temporary pointer that cannot be manipulated

Excerpt from: http://blog.csdn.net/zhangfn2011/article/details/6364750

C_str ()

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.