I'm trying to assign a string to a struct value, which works, but the value somehow links to the variable:
string tmp;
struct test_struct{
const char *server, *user;
};
test_struct test;
tmp = "foo";
test.server = tmp.c_str();
cout << test.server << endl;
tmp = "bar";
test.user = tmp.c_str();
cout << test.user << endl;
cout << "" << endl;
cout << test.server << endl;
cout << test.user << endl;
The output is:
foo
bar
bar
bar
Can someone please explain why this is happening?