First – find out how to run this code under a debugger. Then it’ll just stop on the line where the segfault occurs, which should make it more-obvious what the problem is. Given that you’re saying “segfault”, it’s probably safe to assume you’re running some unix-variant, in which case, “gdb” is probably your debugger.
Second – a segfault is, maybe 90% of the time, due to overrunning the bounds of allocated memory. Since your program isn’t allocating any memory with “malloc”, that means you’re writing past the end of one of your arrays. Chances are, one of your loops is running way too many times, and accessing past the end of an array.
1
solved Segfault in c program