Both of your examples work but they are unsafe, since the memory they point to is disallocated stack space.
You can safely return local primitives by-value but you cannot safely return any local variables by-reference, that is to say, by pointer.
When local integers and floats go out of scope, they are not specifically deleted, more abandoned and left to perish in reusable stack space. Local strings suffer a similar fate. They live in heap space, which is specifically disallocated by the garbage collector at the end of the procedure. But when a local string is returned, it's name is removed from the garbage collector's 'death' list, and it is spared the fate of the other local strings.
In my example, temp lives in heap space, and is not on the garbage collector's list. So it remains there until you delete it.
Charles