You passed a pointer by value. This means that the pointer this function works with is a local copy of the caller’s pointer. And therefore, the value that you assign to the function cannot be seen by the caller.
Pass it by reference instead, so that the function can assign the caller’s pointer, rather than assign a local copy.
void func1(int* &ptr)
Or perhaps consider returning the newly allocated pointer:
int* func1()
{
return new int[5];
}
I would prefer the latter approach.
source
share