C - 尝试制作哈希表时出错

C - Errors when trying to make a hashtable

我正在研究一个散列 table,它将字符串存储在链接列表中,这样我就可以避免冲突。但是,我收到两个错误,我不确定如何修复。我得到的第一个错误是在 NewT->Table[i] == NULL; 行中。意思是 warning: statement with no effects [-Wunused-value].

我遇到的第二个错误是在同一个函数中。错误在 return NewT 行,错误是 warning: return from incompatible pointer type[enabled by default]。我已经盯着这个看了一段时间,但我看不到哪里有未使用的值,而且即使经过一些研究,我也不知道 return 错误是什么意思。有人可以向我解释这些并帮助我解决它们吗?

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

#define HASH_MULTIPLIER 65599

/*Structures*/
typedef struct List_T 
{
    char *str;
    int count;
    struct List_T *next;
} ListT;

typedef struct Hash_T
{
    int htsize;
    ListT **Table;
} HashT;

/*Prototypes*/
unsigned int hash(const char *str);
HashT **ht_create(void);

int htsize;

int main(int argc, char *argv[])
{
    if (argc <= 1)
    {
        printf("Please declare a table size");
        return 1;
    }
    htsize = atoi(argv[1]);
    return 0;
}
unsigned int hash(const char *str)
{
    int i;
    unsigned int h = 0U;

    for (i = 0; str[i] != '[=10=]'; i++)
        h = h * HASH_MULTIPLIER + (unsigned char) str[i];

    return h % htsize;
}

HashT **ht_create(void)
{
    HashT *NewT;
    int i;

    if (htsize < 1) //invalid size for
    {
        fprintf(stderr,"Invalid Size for table");
        exit(0);
    }

    if ((NewT = malloc(sizeof(HashT))) == NULL)
    {
        fprintf(stderr,"Invalid size for table");
        exit(0);
    }

    if ((NewT->Table = malloc(sizeof(ListT *) * htsize)) == NULL)
    {
        fprintf(stderr,"Invalid size for table");
        exit(0);
    }

    for (i = 0; i<htsize; i++)
    {
        NewT->Table[i] == NULL;
    }

    NewT->htsize = htsize;

    return NewT;
}

The first error I am getting is in the line that says NewT->Table[i] == NULL;. It's saying warning: statement with no effects [-Wunused-value].

出现此错误是因为代码正在进行比较,而不是赋值。比较返回的值(Table[i] null?)本身没有分配给任何其他东西,这意味着它未被使用。

保留单个 = 运算符而不是双倍的 == 以确保您实际上是在赋值而不是比较。

The second error I'm getting is in the same function. The error is in the line return NewT and the error is warning: return from incompatible pointer type[enabled by default].

你的函数声称要返回一个指向 HashTHashT ** 的指针,但你最终返回了一个指向 HashTHashT * 的指针相反,这是您的 NewT 变量的类型。

您的函数签名应该使用一个 * 而不是两个。