C++ - char* vs. string*

前端 未结 7 1530
清歌不尽
清歌不尽 2021-02-04 03:28

If I have a pointer that points to a string variable array of chars, is there a difference between typing:

char *name = \"name\";

7条回答
  •  醉话见心
    2021-02-04 03:55

    C++ string class is encapsulating of char C-like string. It is a much more convenient (http://www.cplusplus.com/reference/string/string/).

    for legacy you always can "extract" char pointer from string variable to deal with it as char pointer:

        char * cstr;
        string str ("Please split this phrase into tokens");
        cstr = new char [str.size()+1];
        strcpy (cstr, str.c_str());    //here str.c_str() generate null terminated char* pointer
        //str.data() is equivalent, but without null on end
    

提交回复
热议问题