C 用于存储字符串的哈希表实现

C 用于存储字符串的哈希表实现,c,hashtable,C,Hashtable,这是我存储字符串值的哈希表的代码。要在“insert”函数中使用线性探测,我需要检查指针在该特定哈希值处是否为空。我还没有完成insert函数,但我被卡住了,因为当我检查insert函数中的if(_hash_table[n]==NULL)时,它没有进入分支。在对值进行散列之前,如果我打印“the_hash_table[1]”,它会打印“faz”,但在打印之后,它会打印一些奇怪的字符。我哪里出错了 #include <stdio.h> #include <stdlib.

这是我存储字符串值的哈希表的代码。要在“insert”函数中使用线性探测,我需要检查指针在该特定哈希值处是否为空。我还没有完成insert函数,但我被卡住了,因为当我检查insert函数中的if(_hash_table[n]==NULL)时,它没有进入分支。在对值进行散列之前,如果我打印“the_hash_table[1]”,它会打印“faz”,但在打印之后,它会打印一些奇怪的字符。我哪里出错了

 #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    /*
    creates a hash table of size 10
    */

    char** create_hash_table(){



        char* the_hash_table[10];   // defines a hash table to store strings

        *the_hash_table=malloc(sizeof(char*)*10); // allocates memory in the heap for the hash table
        int i;
        for(i=0;i<10;i++){ // this loop initializes the string pointers to NULL at the starting point of the hash table
            the_hash_table[i]=NULL;
        }
        return &the_hash_table; // returns the address of the hash table to the main memory

    }

    /*
    this is a method to insert a string into the relevant position of the hash table
    */

    void insert(char* the_string,char** the_hash_table){

        printf("%s",the_hash_table[1]);
        int n=hash(the_string);
        printf("%s",the_hash_table[1]);
        if(the_hash_table[n] == NULL)
            the_hash_table[n]=the_string;

    }
#包括
#包括
#包括
/*
创建大小为10的哈希表
*/
字符**创建哈希表(){
char*the_hash_table[10];//定义一个哈希表来存储字符串
*_hash_table=malloc(sizeof(char*)*10);//为哈希表分配堆中的内存
int i;

对于(i=0;i您没有正确分配内存

您将一个自动变量,
_hash_表
定义为指针数组。分配一些内存并将指向该内存的指针放在数组中。立即用空指针覆盖该指针(以及
_hash_表
的其他元素)

然后返回一个指向本地数组的指针,但一旦函数退出,数组就不再存在。从函数中定义的自动变量返回一个指针总是错误的

你应该做的是:

char** create_hash_table(void) {
    char** the_hash_table = malloc(sizeof(*the_hash_table) * 10);
    for (int i = 0; i < 10; ++i) {
        the_hash_table[i] = NULL;
    }
    return the_hash_table;
}
char**create\u hash\u表(void){
char**the_hash_table=malloc(sizeof(*the_hash_table)*10);
对于(int i=0;i<10;++i){
_散列_表[i]=NULL;
}
返回\u散列\u表;
}
因此,
u hash\u table
是一个指向分配内存的局部变量。您返回其值,即分配内存的地址。然后在
main
中,您将
释放(u hash\u table)
而不是
释放(*u hash\u table)


另外,在
hash
函数中,复制字符串没有意义:只需从
字符串[i]
中读取字符即可。即使复制它有意义,您为此创建的缓冲区也太小了1个字节,它需要是
strlen(字符串)+1
因为strlen返回的长度不包括终止字符串的0字节。

您没有正确分配内存

您将一个自动变量,
_hash_表
定义为指针数组。分配一些内存并将指向该内存的指针放在数组中。立即用空指针覆盖该指针(以及
_hash_表
的其他元素)

然后返回一个指向本地数组的指针,但一旦函数退出,数组就不再存在。从函数中定义的自动变量返回一个指针总是错误的

你应该做的是:

char** create_hash_table(void) {
    char** the_hash_table = malloc(sizeof(*the_hash_table) * 10);
    for (int i = 0; i < 10; ++i) {
        the_hash_table[i] = NULL;
    }
    return the_hash_table;
}
char**create\u hash\u表(void){
char**the_hash_table=malloc(sizeof(*the_hash_table)*10);
对于(int i=0;i<10;++i){
_散列_表[i]=NULL;
}
返回\u散列\u表;
}
因此,
u hash\u table
是一个指向分配内存的局部变量。您返回其值,即分配内存的地址。然后在
main
中,您将
释放(u hash\u table)
而不是
释放(*u hash\u table)


另外,在
hash
函数中,复制字符串没有意义:只需从
字符串[i]
中读取字符即可。即使复制它有意义,您为此创建的缓冲区也太小了1个字节,它需要是
strlen(字符串)+1
因为strlen返回的长度不包括终止字符串的0字节。

这非常有用。现在我知道哪里出错了。谢谢。:)这非常有用。现在我知道哪里出错了。谢谢。:)