You aren't respecting variable scope
. One useful reference about this, here.
For example, in reverse()
, you refer to len
in the for
loop. However, len
is defined in main
, and therefore isn't available to reverse()
. (Does this even compile?)
The value you're returning from reverse()
is a pointer to a stack value that is only in scope in that function. While the calling function, main()
, will end up with a valid pointer, the information that the pointer is referring to has dropped out of scope when reverse()
returned, and is therefore volatile, subject to change. For this simple example, you could return the reversed string by value, rather than by reference. (Send back the array of characters, not a pointer to them.)