There is not anything wrong with doing that.
Except that it hides the warning that you'll get if there is something wrong. Like forgetting to include stdlib.h. That happens to me all the time. I use malloc() but forget to include stdlib.h. If I automatically put a cast on malloc() then it would be that much harder to find the bug.
In C it is unnecessary, but not illegal. In C++ it is required.
A bunch of people will tell you that if you want C++, write good C++. I'm not one of those people because I write libraries that need to be used in both languages. But that doesn't mean I cast my malloc() calls right away. I always test on C first to ensure that it works on the less feature rich language. Then I port to C++ and test there to take advantage of the stricter type checking. Then I test on C again to make sure that the port didn't break anything.
I've caught countless bugs that would go unnoticed if I weren't so anal about correctness. And several of those bugs would come from casts that are unnecessary in C, but required in C++. Cast when you need to, but I believe in working with the compiler, not fighting against it. And working with the compiler means taking advantage of the errors and warnings that it can give you to make your code better.
Am I against casting malloc()? Hell no. But I am against casting malloc() right away just because you "know" it'll be needed when/if you port to C++. I don't cast malloc() unless I'm writing something that needs to be both C and C++ right now. Porting C to C++ isn't that hard, so if my C code isn't going to be used as C++ but might be in the future, I postpone the port until that time.
so what do I have to do next?
It's instructive to convert recursion to iteration using a stack. That's not the best way to solve the Hanoi problem, but you can apply what you learn from it elsewhere. That's a good thing. But if you spend some time thinking, you can come up with a bunch of ways to solve the problem without even considering the recursive solution. Here's on fun solution. See if you can figure it out:
Code:
#include <stdio.h>
#define N 3
int main() {
int d[N + 2], s[N + 2];
int i, dir = N % 2, temp;
for (i = 0; i <= N + 1; i++) {
d[i] = 1;
s[i] = i + 1;
}
while (1) {
i = s[0];
if (i > N) break;
temp = (d[i] + (i % 2 ? dir : 1 - dir)) % 3 + 1;
printf("%d from %d to %d\n", i, d[i], temp);
d[i] = temp;
s[0] = 1;
s[i-1] = s[i];
s[i] = i + 1;
}
}
But I don't recommend turning it in as homework, if this is a school assignment. It's advanced enough to get you a failing grade at the least, and expelled for cheating at the most.