... and you still return a dangling pointer.
No matter how you implement the loop, if the array is allocated on the stack, and a pointer to that array is returned, it will point to memory that is no longer allocated to the stack by the time you use it, and thus, can be literally any sequence of bytes that happens to be physically in those memory locations at the time.