Before you leap for joy, I have a few words of caution:

You should free() what you malloc().
As nadroj has noted, you should not cast the return value of malloc (unless you need your code to be...