2015-11-11 34 views
1

我正在處理鏈表中存儲字符串的散列表,以便避免衝突。但是,我收到了兩個我不確定如何解決的錯誤。我得到的第一個錯誤是在表示NewT->Table[i] == NULL;的行中。這是說warning: statement with no effects [-Wunused-value]C - 嘗試創建散列表時出錯

我得到的第二個錯誤是在相同的功能。錯誤位於行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; 
} 
+0

在分配中刪除一個=。您正在返回NewT,但函數定義中的返回值指示它應該是指向指針的指針,我的c有點生疏,但我認爲它應該返回&NewT或將簽名更改爲僅使用一個*。 – aggaton

+1

@aggaton:你說得對,雖然你可以給我投票 - 確實是無恥的廣告:D – ray

+0

對不起,當我從列表中抽出這個問題時,我以爲我是唯一回答:) – aggaton

回答

2

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].

你的功能聲稱指針的指針被返回HashT,或HashT **,但你最終的指針返回HashT,或HashT *相反,這是你的NewT變量的類型。

您的函數的簽名應該使用一個單獨的*而不是兩個。

+0

當我改變函數簽名到單個'*'我得到一個錯誤,說'錯誤:'ht_create''的衝突類型。任何其他提示?我嘗試了其他用戶建議的返回語句'return &NewT;',但是也出現了錯誤。還有其他建議嗎? –

+0

@donutjuice:那你似乎還有其他問題。我只是通過從前向聲明*和*函數定義中刪除單個'*'來嘗試更改,並且它爲我構建時沒有錯誤。 (我以前只讀過代碼,沒有試過。) – ray