【c++】string與char的轉化

1.string轉char

     string pp = "dagah";
     char p[8];
     int i;
     for( i=0;i<pp.length();i++)
         p[i] = pp[i];
     p[i] = '\0';
     printf("%s\n",p);
     cout<<p;


2.char*轉string

string s;
char *p = "hello";//直接賦值
s = p;


3.string轉char*

string str = "hello";
const char* p = str.data();

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章