First: your code look like C++ more than C.
Second: use proper indentation, it help reading the code.
Third: this code is not autonomous, we can't compile it to see what it does.
you have hard time understanding what the code does and why it crashes.
Use t(he debugger, it will allow you to see the code running and see variables changing. For every step look at variables changes and ask yourself if it match your expectations.
-----
You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.
Debugger - Wikipedia, the free encyclopedia[
^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[
^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.