There is one mistake here:
while (fgetc(ptr) != EOF)
doesn't just work as a condition, but it does read a char
and moves on, so next time you read a string
it's gonna miss the first char
of the word because it was read by fgetc()
. Fix that and report back. I would suggest you use the fscanf()
in your loop condition instead (but then remove it from inside the body of the loop).
Also, you would be better of declaring table
as node* table[n]
, so that the members of table
are just pointers to node
s, not node
s themselves. In fact, you never use the node
s created in your table
. You just use their next
attribute, which is itself a pointer to node
.
Maybe you are trying to read the word stored in table[i]
somewhere in your code, but since there is nothing there you get the segfault
.