modifiing pointer in other function
hi,
everytime i think i fully understand pointers it doesn't take a day for c++ to show me i'm not ;)
here's what I tried: main function decleares a pointer and the create-function should allocate memory and modify that pointer to point to that location.
Code:
#include <iostream>
#include <string>
using namespace std;
void create(string* x)
{
cout << "adr x before: " << x << endl; // 0xbfb4c2fc
x=new string("aaa");
cout << "adr x after: " << x << endl; // 0x804a008
}
int main()
{
string s;
cout << "adr s bevore: " << &s << endl; // 0xbfb4c2fc
create(&s);
cout << "adr s after: " << &s << endl; // 0xbfb4c2fc
return 0;
}
I think I know why this can't work as wanted (because the given pointer-parameter can't be modified right?) I think it should work using the pointer as return, but let's assume the return of the create function is needed for error checking.
The main question is: that is the way to make it work as wanted? do I have to pass a pointer to the pointer? but how? ...
thank you for comments.