如何以这种方式转换我的char类型的缓冲区,我可以在我的哈希函数中使用它 [英] How do i convert my buffer of type char in such a way i can use it in my hashfunction
问题描述
嘿伙计们!
我正在遵循cs50课程,本周我们有拼写错误。每次我尝试加载我的字典时,都会出现错误:
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:
/**
* 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.
推荐答案
只需在调用哈希函数时使用强制转换,例如:
Just use a cast on your call to the hash function, something like:
char text[] = "abcdef";
hash((unsigned char *)text);
您可以通过转换为预期类型来解决此问题:
You can solve this by casting to the expected type:
int wordvalue = hash((unsigned char *)buffer);
< br $> b $ b
另一种选择是在你的 hash()
函数中执行强制转换:
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;
}
请注意,我已使用类型 unsigned char
作为变量 c
这里也是。虽然这不是必需的,但它更清楚地表明使用了无符号值。
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.
这篇关于如何以这种方式转换我的char类型的缓冲区,我可以在我的哈希函数中使用它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!