0

I tried to compile "speller" to only test my load function (I'm not sure it's correct!). I haven't done the other 3 functions yet (check, size and unload). But clang says there is an error with the check function, that I did not modify. Can you please explain me this error ? Thanks in advance.

// Implements a dictionary's functionality
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <ctype.h>`enter code here`
#include "dictionary.h"

// Returns true if word is in dictionary else false
bool check(const char *word)
{
    return false;
}

// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
    // open the dictionary file
    FILE *dict_ptr = fopen(dictionary, "r");

    // Creates the root
    node *root = malloc(sizeof(node));

    // Initialize the trav to root
    node *trav = root;

    // Finds a word in the file and add it to the tree
    for (char c = fgetc(dict_ptr); c != EOF; c = fgetc(dict_ptr))
    {
        // If new line, then it's a word and reinitializes trav to root for next character
        if (c == '\n')
        {
            trav->is_word = true;
            trav = root;
        }
        // If the way is open, move to the next node
        else if (trav->children[getIndex(c)] != NULL)
        {
            trav = trav->children[getIndex(c)];
        }

        // If it points to nothing, then creates a new node
        else if (trav->children[getIndex(c)] == NULL)
        {
            node *newNode = malloc(sizeof(node));
            trav = newNode;
        }
    }
    return true;
}

// Returns number of words in dictionary if loaded else 0 if not yet loaded
unsigned int size(void)
{
    // TODO
    return 0;
}

// Unloads dictionary from memory, returning true if successful else false
bool unload(void)
{
    // TODO
    return false;
}



// Get the index for trie
int getIndex(char c)
{
    if (c == '\'')
    {
        return 26;
    }
    else
    {
        return tolower(c) -'a';
    }
}

Terminal:

~/workspace/pset5/speller/ $ make speller
clang -fsanitize=signed-integer-overflow -fsanitize=undefined -ggdb3 -O0 -Qunused-arguments -std=c11 -Wall -Werror -Wextra -Wno-sign-compare -Wshadow   -c -o speller.o speller.c

clang -fsanitize=signed-integer-overflow -fsanitize=undefined -ggdb3 -O0 -Qunused-arguments -std=c11 -Wall -Werror -Wextra -Wno-sign-compare -Wshadow   -c -o dictionary.o dictionary.c

dictionary.c:10:24: error: unused parameter 'word' [-Werror,-Wunused-parameter]
bool check(const char *word)
                       ^
1 error generated.
make: *** [dictionary.o] Error 1

1 Answer 1

0

That's originally a warning, as the return false; does not use the parameter word. Usually, when you have unused variables or parameters, that's by mistake. You could just put (void)word; on a line before the return to silence it. This line would "use" the parameter without doing anything.

Please note that memory allocated with malloc can initially contain anything. So make sure to initialise the memory. Maybe calloc instead of malloc is an option, this zeroes all the bytes allocated.

And something's wrong around your second malloc in load. You don't seem to install the new node in the trie.

You must log in to answer this question.

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