Memory management
Right now, your code leaks memory--every time you call reverse
, it allocates some memory, and none of your other code frees it again.
If all you're doing is reversing one string, then exiting, that's of little consequence--but if you try to use this in real code, leaking memory like this is generally unacceptable.
const correctness
Since you're not modifying the input string, you might as well use const
in the function's signature:
char* reverse(char const * str);
Buffer overrun protection
Right now you have:
scanf("%s", word);
This is essentially equivalent to gets(word);
. That is to say, it provides absolutely no protection against the user entering a string longer than you've provided space to store. When using %s
with scanf
(or cousins like fscanf
, sscanf
, etc.) you need to specify the maximum length:
scanf(%49s""%49s", word);
Alternatively, consider using fgets
, which also requires you to specify the buffer size.
Note that there's a difference in the size you specify though. With fgets
, you specify the size of the buffer, but with scanf
you specify the size of string it's allowed to read, which is one less than the size of the buffer itself. Also note that fgets
normally retains the \n
at the end of what was entered though (if you get data without a \n
on the end, it means the buffer you supplied wasn't large enough to hold all the data that was entered).