0
// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
    int i;
    for (i = 0; i < 5382; i++)
    {
        table[i] = NULL;
    }

    int hash_func(char *str);
    FILE *file = fopen(dictionary, "r");
    for (i = 0; i < 143091; i++)
    {
        char ch;
        ch = 0;
        int size_of_word;
        size_of_word = 0;
        char *temp_string;
        temp_string = NULL;
        while ((ch = fgetc(file)) != 92)
        {
            sprintf(temp_string, "%c", ch);
        }
        int x;
        x = hash_func(temp_string);
        
        //add a node in the hash table
        node *n = malloc(sizeof(node));
        if (n == NULL)
        {
            return 1;
        }
        n->word = temp_string;
        n->next = NULL;
        if (table[x] != NULL)
        {
            node *temp;
            temp = table[x];
            table[x] = n;
            n->next = temp;
        }
        else
        {
            table[x] = n;
        }
    }
    return 0;
}
2
  • please post dictionary.c file – stensal Jun 20 '20 at 22:13
  • do you mean the rest of dictionary.c ? – Thomaslusignan Jun 20 '20 at 22:57
0

Look at this code:

    char *temp_string;
    temp_string = NULL;
    while ((ch = fgetc(file)) != 92)
    {
        sprintf(temp_string, "%c", ch);

temp_string is a pointer var, pointing at NULL. The sprintf line fails because sprintf cannot print to NULL. It needs a legitimate memory address to write to.

You could use a pointer, and allocate memory, but why? You already know this will be a temporary buffer to be reused over and over. Why not just allocate a char array long enough to hold the longest word? char temp_string[some appropriate lenght];

Side note: In the future, please include context and details about the problem. Cite specific results and specific lines of code causing problems. A title with little descriptioin and a question body with only the code is called a naked code drop and is against forum rules. Context and detail must be added.

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

You must log in to answer this question.

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