0

To start off, I'm not even sure if the reasoning behind this is correct because I felt totally lost at the beginning of the pset, but the program won't even compile because of this error:

incompatible pointer types passing 'char *[46]' to parameter of type   'const char *' [-Werror,-Wincompatible-pointer-types]
            strcpy((new_node->word), word);

My code is below:

// define the structure of linked-list node for later use
typedef struct node 
{
    // word to be stored in the node
    char word[LENGTH + 1];
    // pointer to the next node in the list
    struct node* next;
}
node;

// initialise the array of pointers == basis for hash table
struct node* hashtable[HASHTABLE_SIZE] = {};

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

/**
 * Loads dictionary into memory.  Returns true if successful else false.
 */
bool load(const char* dictionary)
{ 
// initialise index for hash function --> use later
int index = 0;
// create an array to put the read word into
char* word[LENGTH + 1] = {};

// open the dictionary file to read from
FILE* fp = fopen(dictionary, "r");
if(fp == NULL)
{
    return false;
}
else 
{
    // repeat until end of file
    while(!feof(fp)) 
    {
        // read in word from dictionary
        fscanf(fp,"%s",*word);

        // use a hash function to map word to index in the hash-table array
        index = hash_function(*word);

        // if the index pointer is empty, we need to create a new node and put it in front
        if(hashtable[index] == NULL)
        {
            // build the new node
            node* new_node = malloc(sizeof(node));
            strcpy((new_node->word), word);
            new_node->next = NULL;
        }
        // if a node with that index already exists, separate chaining
        else
        {
            // build new node
            node* new_node = malloc(sizeof(node));
            strcpy((new_node->word), word);
            new_node->next = NULL;

            // use separate chaining...I guess :P
            // traverse the list
            node*prev = NULL;
            node* head = NULL;
            for(node* cur = head; cur != NULL; cur = cur->next)
            {
                // compare words inside the list, letter by letter
                for(int i = 1; i <strlen(new_node->word); i++)
                {
                    if(cur->word[i] > new_node->word[i])
                        break;
                    else 
                        prev = cur;    
                }
            }
            // if node belongs at head, prepend
            if(prev == NULL)
            {
                new_node->next = head;
                head = new_node;
            }
            // else insert in middle or end
            else
            {
                new_node->next = prev->next;
                prev->next = new_node;
            }
        } 

    }

}
fclose(fp);
return true;
}

int hash_function(char* key)
{
    int index = 0;
    if(key != NULL)
    {
        int first_letter = toupper(key[0]);
        index = (first_letter - 97) % HASHTABLE_SIZE;
        return index;
    }
}

The hash function obviously needs to be as I made it with a very simple and small dictionary in mind, but it would be great if you could give me some general comments and hints concerning the compiler error :)

1 Answer 1

2

I think the problem might be here:

char* word[LENGTH + 1] = {};

If you use the star and the brackets, you are declaring an array of pointers to chars. Try instead just

char word[LENGTH +1] = {};

When your code tries to copy your array of pointers into the node's word, it realizes they aren't the same type?

I might be wrong, but give it a shot.

HTH


If this answers your question please accept it by clicking the gray check-mark to the left, so that it becomes green. You can also vote it up by pressing the up arrow above the check-mark.

You must log in to answer this question.

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