C_str () function

Source: Internet
Author: User

#include <string.h>

const char *C_STR ();

Returns a string address, which is a C function that returns the type const char*
The C_STR () function returns a pointer constant 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 ()

It's not right.

char* C;
String s= "1234";
c = S.c_str ();

This should be used:

Char c[string s="1234"

C_STR () returns a string containing string in char* form
If a function requires the char* parameter, you can use the C_str () method:
string s = "Hello world!";
printf ("%s", S.c_str ()); Output "Hello world!"

The. C_STR () is used where char* is needed. If you just need to deal with strings, you don't need this at all.

However, all C-language functions and some IO-related functions of C + + (such as the constructor called when creating a file stream object) require a string with a char*-type parameter and do not support string types, which can be used to get a char* pointer with the. C_STR () of a String object. .

C_str () function

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.