This is the entire program:
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
const char* str_c = "\0";
const char* function() {
string str_cpp = "John Doe";
str_c = str_cpp.c_str();
cout << str_cpp.c_str() << "\n";
cout << str_c << "\n";
return str_c;
}
int main() {
cout << function() << "\n\n";
system("PAUSE");
return 0;
}
And this is what it returns:
- John Doe
- John Doe
- ╠╠╠╠╠╠╠╠
I have no clue why the string to which str_c
points suddenly went missing. Is it because its contents were created within function()
and therefore had a shorter lifespan?