string char* char[]互相转换

string to char*

std::string str = "string";
char* chr = const_cast(str.c_str())
string str = "some string" ;
char *cstr = &str[0];
std::string str = "string";
const char *cstr = str.c_str();
std::string str = "string";
char *cstr = new char[str.length() + 1];
strcpy(cstr, str.c_str());
// do stuff
delete [] cstr;

char* to string

char c[] = "this is a char array";
const char* t = "const char";
string s = t;
string ss = c;
const char* cc = "this is a const exp";
string s(cc, cc + strlen(cc));

char* 和const char*

const char* src = "this is a const exp";
char* ch = const_cast(src);
const char* dst = static_cast(ch);
cout << ch << endl;
cout << dst << endl;

string转char[]

	string s="string";
	char ch[20];
	strcpy(ch,s.c_str());

char[]转string

char ch[6]="string";
string s=ch;

char*转char[]

char *s="string";
char  ch[100];
strcpy(ch,s);

你可能感兴趣的:(C++)