You are returning a pointer to a local variable that doesn't exist anymore when the function returns. You have to malloc storage for it and return that. Alternatively, you can let the caller pass in a buffer to be filled. In any case the caller is responsible for freeing the memory later.