OK, so I see a lot of problem here.
First, madscientist posted in the C++ section (not C!), and specifically asked for help in C++ (not C!).
Secondly, cauberong09 dumped a solution to a problem directly (which is discouraged), which has a lot of defects (for example, undefined behavior), and finally posted code in C (not C++!).
So let's clear this up now.
Is this supposed to be C or C++?
C does not belong in the C++ forum and vice versa. Therefore, it is strongly discouraged to post examples or code in C in the C++ forum or vice versa. If this is indeed supposed to be C++, then cauberong09's code does not belong here, nor should it have been posted in the first place.
If this is indeed C, then this post is in the wrong forum.
Also, cauberong09. It is understandable that you want to learn more, and that is fine. I would recommend that you actually read people's problems and the answers to them instead of dumping code. You will learn a lot. I guarantee it.