This is usually not a problem, but there are things to consider. This is usually a question of const-correctness, which means keeping track of what you can change and what you cannot.
If you return a double-quoted string, it is const char * and processes it, like everything else, is an invitation to trouble. Changing such a line is undefined behavior, but usually leads to the failure or change of this line, wherever it is mentioned.
If you return an array of characters on the stack (i.e. the local variable of the called function), it will leave and the pointer will not point to anything, in particular, possibly with bad results at some time.
If the called function returns what is already const char * , then a cast is required to change it to char * . In addition, if you are really going to change it, you must be sure that it is volatile. It is usually better to save it as const char * .
There is no problem with the return memory allocated using malloc() or new , but you have a ownership problem: what function should free() / delete when, and what will you do with the possible copy? This is where C ++ smart pointers shine.
David thornley
source share