Right.
Your code works, but leaks memory. You
could solve that by doing this:
1 2 3 4
|
char *myName = getName();
cout << "Hello " << myName << endl;
delete[] myName; // <- delete it
return 0;
|
But that's not something you want to get in the practice of doing.
This is "passing ownership" and leads to very hard-to-find problems unless you are extremely careful. In general when you allocate something with new[], whoever allocated it should be responsible for cleaning it up / deleting it. In this case, getName allocated it, but is leaving it up to whatever function called it to clean it up. Hence, getName is "passing ownership" of the buffer to main().
(That's not to say that passing ownership is always bad. Sometimes it's the best way to go. Here, though, I don't recommend it. Especially since the problem is easily solved with strings)
That's besides the point that char arrays are bad for user input because they are easily overflowed. For example your code will explode if the user enters a name longer than 24 characters.
Strings are safer and easier. Use them.