Char *, const char * and string Conversion, charconst

Source: Internet
Author: User

[Conversion] char *, const char * and string Conversion, charconst

1. Convert string to const char *

String s = "abc ";

Const char * c_s = s. c_str ();

2. Convert const char * to string

Assign a value directly.

Const char * c_s = "abc ";

String s (c_s );

3. Convert string to char *

String s = "abc ";

Char * c;

Const int len = s. length ();

C = new char [len + 1];

Strcpy (c, s. c_str ());

4. Convert char * to string

Char * c = "abc ";

String s (c );

5. Convert const char * To char *

Const char * cpc = "abc ";

Char * pc = new char [100]; // long enough

Strcpy (pc, cpc );

6. Convert char * to const char *

Assign a value directly.

Char * pc = "abc ";

Const char * cpc = pc;

 

Address: http://blog.sina.com.cn/s/blog_5436b2f40100pjzz.html

 

You can also study the conversion relationships between char * And wchar_t *, wstring, WCHAR, PSTR, LPSTR, PWSTR, LPWSTR, LPCSTR, LPTSTR, LPCTSTR, CString, and so on ......

2011-4-2 00:32

String to char * string str ("transfer ");
Char * ch = const_cast <char *> (str. 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.