string、char *、char[] 相互转换转换

1.string-->char *,

 调用string的方法c_str()或者data();

   string str = "abc";
   char *pChar = (char *)str.data();

2.char *-->string

    char *pTemp = "abcdef";
    string tmpStr(pTemp);

3.string-->char[]

    string str = "abcdefg";
    char p[10];
    int i = 0;
    for (i = 0; i < str.length(); i++)
    {
        p[i] = str[i];
    }
    p[i] = '\n';    

4.char[]-->string

    char p[10] = "abcdefg";
    string str1 = p;    

 

posted @ 2020-07-22 18:52  zwj鹿港小镇  阅读(124)  评论(0编辑  收藏  举报