1. string转const char*
string s = "abc";
const char* c_s = s.c_str();
2. const char*转string
直接赋值即可
const char* c_s = "abc";
string s(c_s);
3. string转char*
string s = "abc";
char* c;
const int len = s.length();
c = new char[len+1];
strcpy(c,s.c_str());
4. char*转string
char* c = "abc";
string s(c);
5. const char*转char*
const char* cpc = "abc";
char* pc = new char[100];//足够长
strcpy(pc,cpc);
6. char*转const char*
直接赋值即可
char* pc = "abc";
const char* cpc = pc;
原文地址:http://blog.sina.com.cn/s/blog_5436b2f40100pjzz.html
妳还可以研究研究char*和wchar_t*、wstring、WCHAR、PSTR、LPSTR、PWSTR、LPWSTR、LPCSTR、LPTSTR、LPCTSTR、CString等等诸类型间的转换关系……
2011-4-2 00:32
string 转 char* string str ("transfer");
char * ch = const_cast<char*>(str.c_str())
char * ch = const_cast<char*>(str.c_str())
2012-11-2 12:16