C ++ Function Learning: c_str ()

Source: Internet
Author: User

(Content is extracted from the network and sorted by yourself)

const char* c_str ( ) const;
String: c_strpublic member function
const char* c_str ( ) const;

Get C string equivalent

Generates a null-terminated sequence of characters (c-string) with the same content as the string object and returns it as a pointer to an array of characters.

A terminating null character is automatically appended.

The returned array points to an internal location with the required storage space for this sequence of characters plus its terminating null-character, but the values in this array shoshould not be modified in the program and are only guaranteed to remain unchanged until the next call to a non-constant member function of the string object

 

The standard header file <cstring> contains a function library that operates C-strings. These library functions express almost every string operation we want to use. When a library function is called, the client program provides a string type parameter, while the library function uses a C-string internally. Therefore, the string object must be converted to a char * object, c_str () provides such a method that returns the pointer to the character array of the const char * type (readable and unchangeable. Example:

# Include <iostream>
# Include <string. h>

Using namespace STD;
Int main (void ){

String add_to = "Hello! ";
Const string add_on = "baby ";
Const char * cfirst = add_to.c_str ();
Const char * csecond = add_on.c_str ();
Char * Copy = new char [strlen (cfirst) + strlen (csecond) + 1];
Strcpy (copy, cfirst );
Strcat (copy, csecond );
Add_to = copy;
Cout <"Copy:" <copy <Endl;
Delete [] copy;
Cout <"add_to:" <add_to <Endl;
Return 0;
}

 

Output:

Copy: Hello! Baby
Add_to: Hello! Baby

 

Note:

The _ STR () function returns a pointer to a regular C string with the same content as this string.

This is to be compatible with the C language and has no string type in the C language. Therefore, you must use the member function c_str () of the string class object to convert the string object to the string style in C.

Note: Use the strcpy () function to operate the pointer returned by the c_str () method.

For example, it is best not:

Char * C;

String S = "1234 ";

C = S. c_str ();

// C finally points to the content of spam, because the S object is destructed and its content is processed (correct: the structure of the S object is carried out after the assignment operation is completed on the pointer C, so there is no error)

 

Cause of error prompted in VC ++ 2010: error: the "const char *" type value cannot be allocated to a "char *" type object.

It should be used as follows:

Char C [20];

String S = "1234 ";

Strcpy (C, S. c_str ());

In this way, no error occurs. c_str () returns a temporary pointer and cannot be operated on.

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.