I've only skimmed the thread, but I don't think anyone has properly explained this, so I'll have a go.
The
char* is being returned just fine. The problem is that you don't seem to understand properly what a
char* is.
A
char* (or an
int*, or an
Anything*) is a pointer.
And a pointer is nothing but a single number. That number is a memory address. When we say that the pointer "points to" something, we mean that it stores the address of the memory where that something is stored.
The tricky thing when using pointers is that you have to make sure that the address stored in the memory has been properly reserved to store the data you're pointing to, for as long as you want to point to it.
In your original code, the memory that
newstr points to, is allocated on the stack. That means that, when the function finishes, the memory is freed up; it is no longer reserved for that data. So, your function returns the address of that memory (a pointer), but when the calling code tries to use that address, that memory is no longer storing the data you wanted it to store.
This is why using
new is one solution to your problem. By dynamically allocating the memory for the string on the heap, it stays allocated until
you decide to use
delete to free it up again. That memory stays reserved for storing the data that was put there by the function, even after the function has finished, and will remain reserved until you
delete it.
I hope that clears things up?