Hi,
I am facing an issue in string to const char pointer conversion. I am doing stuff in the fun() but here i am just giving my example. Here it the code:
// Helper function
string fun() {
string abc = "Daniweb";
// print here #1
return abc;
}
// main function
int main() {
const char * returnValue = fun().c_str();
// print here #2
string returnValue2 = fun().c_str();
// print here #3
}
When I print the value in fun() function before returning it, it is printing fine. (#1)
When I print the value after const char (#2), it i giving some random values (sometimes empty, adding some characters at the end etc.).
When I print the value after returnValue2, it is working fine in all cases. (#3)
What's the problem with the case #2? Can't we do the way I have done in #2? Please explain.
Thanks in advance.