Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

char* to string*

jedi06jedi06 Posts: 38Member
Is there a way to convert a string pointer to a char pointer?

C++ is ridiculous with this string and char-string stuff!

Comments

  • bilderbikkelbilderbikkel Posts: 754Member
    : Is there a way to convert a string pointer to a char pointer?
    : C++ is ridiculous with this string and char-string stuff!

    Yes, it is ridiculously (pun intended) easy:

    [code]
    //std::string to char*
    const std::string s = "Hello";
    const char * c = s.c_str(); //'c_str' for 'C-style string' or 'character string'

    //char* to std::string
    const char c[] = "World";
    const std::string s(c); //One of these
    const std::string s = c; //One of these
    [/code]

    Have fun,
    bilderbikkel

  • danhgdanhg Posts: 1Member
    : Is there a way to convert a string pointer to a char pointer?
    :
    : C++ is ridiculous with this string and char-string stuff!
    :

    If you find std::string awkward try using this string class instead:
    http://www.programmersheaven.com/download/41104/download.aspx
  • DonotaloDonotalo Posts: 715Member
    : Is there a way to convert a string pointer to a char pointer?
    :
    : C++ is ridiculous with this string and char-string stuff!
    :
    [purple]why are u saying string pointer? C++ string is developed to drive the pointer stuffs away. and it is not a good programming practice to use string* instead of string. do not misuse string.

    std::string is called C++ string and C string is implemented by a null terminated character array. C++ string is easy to use, with the drawback that it is slower than C string. whatever string operation u can do with C string, u can also do it using C++ string more comfortably. an example may be string concatenation - u have strcat for C string (strcat(str1, str2)) and '+' operator for C++ string (str1 + str2).
    [/purple]
    [hr][purple]~Donotalo()[/purple]

Sign In or Register to comment.