【发布时间】:2016-02-12 03:05:24
【问题描述】:
我正在研究一个将字符串存储在链表中的哈希表,这样我就可以避免冲突。但是,我遇到了两个不知道如何解决的错误。我得到的第一个错误是在NewT->Table[i] == NULL; 的行中。上面写着warning: statement with no effects [-Wunused-value]。
我得到的第二个错误是在同一个函数中。错误在return NewT 行中,错误为warning: return from incompatible pointer type[enabled by default]。我已经盯着这个看了一段时间,我看不出哪里有未使用的值,即使经过一些研究,我也不知道返回错误意味着什么。有人可以向我解释这些并帮助我解决它们吗?
#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] != '\0'; 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;
}
【问题讨论】:
-
在作业中删除一个 =。您正在返回 NewT,但是函数定义中的返回表明它应该是指向指针的指针,我的 c 有点生锈,但我认为它应该返回 &NewT 或更改签名以仅使用单个 *。
-
@aggaton:你是对的,虽然你可以给我你的投票——确实是无耻的广告:D
-
很抱歉,当我从列表中找出问题时,我以为只有我一个人回答 :)
标签: c linked-list hashmap hashtable