I honestly can't understand why you would want to do this. Just deal with a std::string until you need a C string, usually as a 3rd party library function parameter.
Compilers would (should) warn us about this (logical) error:
1 2 3 4 5 6 7 8 9 10 11
char* return_c_string( const std::string& )
{
char cstr[100] { };
// ...
// GCC: warning: address of local variable 'cstr' returned
// LLVM: warning: address of stack memory associated with local variable 'cstr' returned
// Microsoft: warning: returning address of local variable or temporary
return cstr;
}
Does it point to something allocated on the heap? Who is supposed to delete it?
Does it point to some file-scope or static memory? How long will it remain valid? If I call the function a second time with different variables, the value will change, right? If it's a pointer to some class's member data then it will disappear when the class instance is destroyed.
If your function returns a pointer then your comments need to make these issues absolutely clear. How long is the data valid? Is the caller responsible for deleting it?
For all of these reasons, it's usually a bad idea to return a pointer.
In your specific case, you could return a string instead of pointer.