Conversion of C + + Char*,const Char*,string,int to each other

Source: Internet
Author: User

Conversion of C + + Char*,const Char*,string,int to each other

1. String to const char*

String s ="abc";
Const Char* c_s = s. C_str();

2. Const char* to a string directly assigns the value can

Const Char* c_s ="abc";
String s(c_s);

3. String Transfer char*

string s = "ABC" char* c constint len =  s. ();
c =new char [len+1];
strcpy (c,s c_str ())

4. char* Turn string

Char* c ="abc";
String s(c);

5. Const char* Turn char*

Const Char* CPC ="abc";
Char* pc =new char[+]; Long enough
strcpy(pc,CPC)

< Span class= "pun" >< Span class= "KWD" >< Span class= "pun" >6.string with int

< Span class= "pun" >< Span class= "KWD" >< Span class= "pun" >string kind_str = "123";

int kind = atoi (Kind_str.c_str ());

Conversion of C + + Char*,const Char*,string,int to each other

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.