C++学习(一):字符类型转换
#include <iostream> using namespace std; int main(){ //1.char[] to char * char c1[] = "aa"; char * c2 = c1; cout << c2 << endl; //2.char * to char[] const char * c3 = "bb"; char c4[] = "cc"; strncpy_s(c4, c3, strlen(c3) + 1); cout << c4 << endl; //3.char * to string const char * c5 = "dd"; string s1 = c5; cout << s1.c_str() << endl; //4.string to char * string s2 = "ee"; const char * c6 = s2.c_str(); cout << c6 << endl; //5.char[] to string char c7[] = "ff"; string s3 = c7; cout << s3.c_str() << endl; //6.string to char[] string s4 = "gg"; char c8[] = "hh"; strncpy_s(c8, s4.c_str(), s4.length() + 1); cout << c8 << endl; //7.char * to const char * char * c9 = "ii"; const char * c10 = c9; cout << c10 << endl; //8.const char * to char * const char * c11 = "jj"; char * c12 = (char *)c11; cout << c12 << endl; system("pause"); return 0; }