I'v spent days trying to fix this, with one bug after the other and still havent been able to solve it. I'd be grateful if anyone can tell me what I'm doing wrong.

Some stuff I found out :-

1) At the end of the load function I tried to print the entire linked list and could print all the words out.

2) The seg fault happens in the strcmp function.

3)While debugging just before strcmp when I print worda it shows some weird characters, not what I expected.

4)The check function works perfectly on the first word, but the seg fault happens in the second word.

And here's all my code till check :-

 * dictionary.c
 * Computer Science 50
 * Problem Set 5
 * Implements a dictionary's functionality.

#include <stdbool.h>
#include <stdio.h>
#include <ctype.h>
#include "dictionary.h"
#include <stdlib.h>
#include <string.h>

typedef struct node{

     struct node* next;
     char word[46];


 * The hash function
int hashy(const char* word)
    int num = tolower(word[0]) - 97;
    return num % 26;

node* letters[26];
 * Returns true if word is in dictionary else false.
bool check(const char* word)

    char* worda = malloc(46); //I tried strlen(word) + 1 too

    //find the wanted bucket
    int buc = hashy(word);

    //loop through word
    for(int i = 0,n = strlen(word);i < n;i++){




     worda[i] = word[i];

     worda[n] = '\0';


    node* crawler = malloc(sizeof(node));

    crawler = letters[buc];

    while(crawler != NULL){

      if(strcmp(crawler->word,worda) == 0){
      return true;

    crawler = crawler->next;


    return false;

 * Loads dictionary into memory.  Returns true if successful else false.
bool load(const char* dictionary)

    FILE* dic = fopen(dictionary,"r");

    int word_count = 0;

    //Make all the next pointers NULL
    for(int j=0;j<26;j++){
     letters[j]= NULL;

    //Check if file could be opened
    if(dic == NULL){

      printf("File not found, I repeat File not found");
      return false;


    for(int i = 0;!feof(dic);i++){

      word_count += i;
      //new node
      node* new_node = malloc(sizeof(node));

      //get the word and store it in the node
      new_node->next = NULL; 

      //first time
      if(letters[hashy(new_node->word)]== NULL){

        letters[hashy(new_node->word)] = new_node;


          node* temp = letters[hashy(new_node->word)];

          letters[hashy(new_node->word)] = new_node;

          new_node->next = temp;




    return true;

Your problem lies in how you are handling the end of the dictionary file. The for loop setup line tests for end of file, but you read the next line of the file and then process it without first testing for eof. The result is that you're trying to process past the end of the file and causing the seg fault.

FOR loops are primarily for processing a known number of passes. WHILE loops are designed for looping until a certain condition is met. Instead of a for loop, why don't you use a while loop to do the reads from the dictionary while testing for EOF?

If this answers your question, please click on the check to accept. Let's keep up on forum housekeeping. ;-)

  • I changed that but it still shows a seg fault. – Jedi18 Oct 25 '15 at 5:56
  • Well, my crystal ball is in the shop this week. Can you edit the question to show the revised code? – Cliff B Oct 25 '15 at 6:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .