Usually, that isn't an issue, but there are things to consider. It's usually a matter of const-correctness, which means keeping track of what you can change and what you can't.
If you're returning a double-quoted string, it's const char *
, and treating it like anything else is an invitation for trouble. Changing such a string is undefined behavior, but will usually cause the program to crash or change that string wherever it's referred to.
If you return a character array on the stack (i.e., a called function's local variable), it will go away, and the pointer will point to nothing in particular, probably with bad results at some time.
If the called function is returning something that's already const char *
, then changing it to char *
requires a cast. Further, if you're actually going to change it, you need to be sure it's changeable. It's usually much better to keep it as const char *
.
There's no immediate problem with returning memory allocated with malloc()
or new
, but you do have the problem of ownership: what function should free()
/delete
it, when, and what do you do about possible copies? This is where C++'s smart pointers shine.