str is a local variable. It is destroyed after the function returns.

You need to either make str static or dynamically allocate memory and return the pointer, just remember to delete[] it.