I just did this same project the other week. If you are on unix you can use GDB to debug exactly where the seg fault is happening. If you don't know about GDB a simple google search will help you a lot. The command backtrace within GDB will show you what you need to know. Also compile with -g to show line numbers