C++ - char* vs. string*

前端 未结 7 1523
清歌不尽
清歌不尽 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
    
    0 讨论(0)
提交回复
热议问题