It's more important that you know how to locate seg faults. This is a very common defect and if you can't locate seg faults, you will be severely handicapped.
There are two common ways. First, insert strategic unique printf statements. A seg fault will occur after the last executed printf and before the first non-executed printf. Just keep moving them closer together until you bracket the failing code with printf statements just before and after it!
Second, you can run the code in a debugger and step through until it fails. The problem here is that sometimes, the mere act of running the code in the debugger can mask the seg fault. Then, it's time to fall back to the first method.
So, with that in mind, it's time to take another crack at finding your seg fault. Once you find it, then we can discuss what is actually causing it, assuming you can't figure it out. ;-)