Click here to Skip to main content
15,891,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey guys!

I'm currrently following the cs50 course and this week we have the misspellings pset. Everytime i try to load in my dictionary, an error occurs:

dictionary.c:93:30: error: passing 'char [47]' to parameter of type 'unsigned char *' converts between pointers to integer types with
      different sign [-Werror,-Wpointer-sign]
        int wordvalue = hash(buffer);
                             ^~~~~~
dictionary.c:38:21: note: passing argument to parameter 'str' here
hash(unsigned char *str)
^

I have no clue what i have to change in order to fix this problem.
Could anyone help me out?!

This is my code so far:

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

#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <stdbool.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include <stdlib.h>
#include <stdbool.h>

#include "dictionary.h"

// create a node with the word and a pointer to the next node
typedef struct node
{
    char word[46];
    struct node* next;
}
node;

// variable to keep track of number of words that have been loaded
int loadedwords;

// define the hashtable
node* hashtable[27] = {NULL};

// hashfunction obtained from http://www.cse.yorku.ca/~oz/hash.html
unsigned long
hash(unsigned char *str)
{
    unsigned long hash = 5381;
    int c = 0;

    while (c == *str++)
    {
        hash = ((hash << 5) + hash) + c;
    }
    return hash;
}

/**
 * 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)
{
    // open the dictionary file
    FILE* dict = fopen(dictionary, "r");
    
    // check if file is valid and opened correctly
    if (dict == NULL)
    {
        printf("Could not open dictionary\n");
        return -1;
    }
    
    // make buffer to store the loaded word plus the NULL terminator
    char buffer[47];
    
    // go through dictionary
    while(fgets(buffer, sizeof(buffer), dict))
    {
        // change the "\n" to "\0" in order to work with NULL
        buffer[strlen(buffer)-1] = '\0';
        
        // create temporary memory space with the size of node
        node* temporary = malloc(sizeof(node));
        
        // node points to the next node and the word
        // copies the buffer content into the location of the temporary pointer
        // temporary node moves on to next node and clears it
        strncpy(temporary ->word, buffer, 46);
        temporary -> next = NULL;
        
        // put word into hashing function to get it's value
        int wordvalue = hash(buffer);
        
        // put temporary node in hashtable if the worldvalue is not known yet in the hashtable
        if(hashtable[wordvalue] == NULL)
        {
            hashtable[wordvalue] = temporary;
        }
        // move over list and put node at the end
        else
        {
            // make a node that points to the beginning of the list
            node* point = hashtable[wordvalue];
            
            // skip over every node untill the next value is NULL
            while (point -> next != NULL)
            {
                point = point -> next;
            }
            point -> next = temporary;
        }
        loadedwords++;
    }
    return false;
}

/**
 * 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.  Returns true if successful else false.
 */
bool unload(void)
{
    // TODO
    return false;
}


Thanks!

What I have tried:

I tried modifying the type of the buffer and i tried to use a different hashfunction.
Posted
Updated 7-Mar-16 7:59am
v3
Comments
Arthur V. Ratz 20-Mar-16 11:05am    
What's the purpose of converting a characters buffer into a hash function in this case ?

Just use a cast on your call to the hash function, something like:
C++
char text[] = "abcdef";
hash((unsigned char *)text);
 
Share this answer
 
You can solve this by casting to the expected type:
C++
int wordvalue = hash((unsigned char *)buffer);


Another option is performing the cast inside your hash() function:
unsigned long hash(char *str)
{
    unsigned long hash = 5381;
    unsigned char c = 0;
 
    while (c == (unsigned char)*str++)
    {
        hash = ((hash << 5) + hash) + c;
    }
    return hash;
}

Note that I have used the type unsigned char for the variable c here too. While this is not really necessary it indicates more clear that unsigned values are used.
 
Share this answer
 
Comments
Arthur V. Ratz 21-Mar-16 2:12am    
5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900