The following code fails as expected because I am passing an invalid pointer:
But if I change func2 to...Code:void func2(char* c, char* d) { c = d; } void func() { char* c = NULL; char* d = new char('f'); func2(c, d); char e = *c; printf(e); } int main(int argc, const char* argv[]) { func(); }
... it works because I'm passing a reference to the pointer.Code:void func2(char*& c, char* d) { c = d; }
I don't really understand the mechanic that allows this to work. Since the pointer is invalid why does passing it as a reference make a difference?
Cheers.




Reply With Quote