string char * const char *之间的互相转换
string -> const char *
用str的c_str()方法或者data()方法均可,这个两个方法返回值为cong char *
string str = "hello"; const char *a = str.c_str(); const char *b = str.c_str();
const char * -> string
const char*转换为 string,直接赋值即可
string -> char *
vs不让用strcpy,只能用strcpy_s代替
string str = "hello"; int str_len = str.length(); char *a = new char[str_len + 1]; strcpy_s(a, str_len + 1, str.c_str()); delete[] a;
char * -> string
直接赋值
const char * -> char *
const char *b = "hello"; char *a = new char[strlen(b) + 1]; strcpy_s(a, strlen(b) + 1, b); cout << a << endl; delete[] a;
char * -> const char*
直接赋值
char *a = "hello"; const char *b = a;