Search code examples
c++stringcstring

String vs C-string in declaring a member variable


I have a member function in a class that returns a String. What do I need to do in the declaration if I want to make the return a C-string type? The revised function may or may not have a C-string argument(It is OK to remain the current argument).

The original declaration is :

string function(string argument);

Solution

  • C-style strings are char arrays closed with a \0, so:

    char* function(string argument);
    

    You may also consider tor return a string and retrieve a C-string from that value via the c_str() method.