2012-03-03 77 views
-2

下面的代碼給我下面的錯誤:請求成員「」不是一個結構或聯合錯誤hashTable中分離鏈

error: request for member ‘name’ in something not a structure or union 
error: request for member ‘name’ in something not a structure or union 
error: request for member ‘data’ in something not a structure or union 
error: request for member ‘next’ in something not a structure or union 

我該如何解決呢? 代碼是:

#define SIZE 5 

typedef struct hashTable{ 
    int data; 
    char *name; 
    struct hashTable *next; 
} table; 


int hash_function(int value) 
{ 
    return value % SIZE; 
} 

int insert(char *inFileName, table ***hashLinked) 
{ 
    FILE *inFile; 
    int val = -1; 
    char str[30]; 
    int probe; 

    if ((inFile = fopen(inFileName, "r")) == NULL) 
    { 
     fprintf(stderr,"Error opening input file, %s\n", inFileName); 
     return -1; 
    } 
    while(fscanf(inFile,"%s %d",str,&val) == 2) 
    { 
     probe = hash_function(val); 

     if(hashLinked[probe] == NULL) 
     {        
      **hashLinked[probe] = malloc(sizeof(table)); 
      **hashLinked[probe]->name = (char *)malloc((strlen(str) + 1) * sizeof(char*)); 
      strcpy(**hashLinked[probe]->name,str); 

      **hashLinked[probe]->data = val; 
      **hashLinked[probe]->next = NULL; 
     }            
     else 
     { 
      table* hashLinkedNode = *hashLinked[probe]; 
      while(hashLinkedNode->next!=NULL) 
      {       
       hashLinkedNode = hashLinkedNode->next; 
      } 
      hashLinkedNode->next = malloc(sizeof(table)); 
      hashLinkedNode->next->name = (char *)malloc((strlen(str) + 1) * sizeof(char*)); 
      strcpy(hashLinkedNode->next->name,str); 
      hashLinkedNode->next->data = val; 
      hashLinkedNode->next->next = NULL; 
     } 
    } 
    fclose(inFile); 
    return 0; 
} 


void printList(BookNode *hd) 
{ 
    for (; hd != NULL; hd = hd->next) 
    { 
     printf("[%s,%d]", hd->name, hd->isbn); 
     if (hd->next) 
      printf(" -> "); 
    } 
    printf("\n"); 
} 

void printHashTable(BookNode **temp) 
{ 
    BookNode *tmp = NULL; 
    int i; 
    for(i=0;i<SIZE;i++) 
    { 
     tmp = temp[i]; 
     while(tmp) 
     { 
      printf("%s %d",tmp->name, tmp->isbn); 
      tmp=tmp->next; 
     } 
    } 
} 

int main(int argc, char *argv[]) 
{ 
    table **hashLinked[SIZE]; 
    insert(argv[1],&hashLinked); 
    printHashTable(**hashLinked); 

    return 0; 
} 
+6

如果您甚至無法編譯您的代碼,如何獲得seg-fault? – 2012-03-03 19:20:33

+1

'表*** hashLinked'?我從來不需要在20年內編寫的任何代碼中使用三重間接。重新開始。 – trojanfoe 2012-03-03 19:23:00

回答

2

一個問題是,你叫insert比你宣佈它的類型以外的東西,

table **hashLinked[SIZE]; 
insert(argv[1],&hashLinked); 

hashLinked是一個指針數組指針table,所以&hashLinked是指向指向table的指針數組的指針的指針,但是insert被聲明爲指向指向table的指針的指針。我不確定自己是否真的明白了你打算做什麼,但似乎合理的是你想要更少的間接水平。我相信通過&hashLinked的原因是你想要hashLinkedinsert中被修改,但是已經通過hashLinked本身完成了,你不需要傳遞它的地址。這會使傳入的類型與聲明的類型兼容,因爲作爲函數參數,hashLinked將成爲指向其第一個元素的指針,即table ***

然後使用不一致的間接計數insert,並獲得*->錯誤,這會導致「會員的東西的要求,是不是一個結構或聯合」錯誤的優先級。 **hashLinked[probe]->name被解析爲**(hashLinked[probe]->name),所以試圖訪問table *name成員,然後解除引用兩次。使用參數類型table ***,正確的訪問將是(*hashLinked[probe])->name,得到table **hashLinked[probe],取消一次得到table *並訪問它的(pointee)成員name。但是,你檢查if (hashLinked[probe] == NULL),如果是這樣

**hashLinked[probe] = malloc(sizeof(table)); 

這是有保證的空指針廢棄。通過檢查和下面的代碼,我相信你實際上想要有一個參數類型table **hashLinked參數是一個鏈接列表table s的數組,這使代碼更容易遵循。在BookNode型灌裝和適應幾個變量和參數,我到達

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

#define SIZE 5 

typedef struct hashTable { 
    int data; 
    char *name; 
    struct hashTable *next; 
} table; 

typedef struct book { 
    int isbn; 
    char *name; 
    struct book *next; 
} BookNode; 

int hash_function(int value) 
{ 
    return value % SIZE; 
} 

int insert(char *inFileName, table **hashLinked) 
{ 
    FILE *inFile; 
    int val = -1; 
    char str[30]; 
    int probe; 

    if ((inFile = fopen(inFileName, "r")) == NULL) 
    { 
     fprintf(stderr,"Error opening input file, %s\n", inFileName); 
     return -1; 
    } 
    while(fscanf(inFile,"%s %d",str,&val) == 2) 
    { 
     probe = hash_function(val); 

     if(hashLinked[probe] == NULL) 
     { 
      hashLinked[probe] = malloc(sizeof(table)); 
      hashLinked[probe]->name = (char *)malloc((strlen(str) + 1) * sizeof(char*)); 
      strcpy(hashLinked[probe]->name,str); 

      hashLinked[probe]->data = val; 
      hashLinked[probe]->next = NULL; 
     } 
     else 
     { 
      table* hashLinkedNode = hashLinked[probe]; 
      while(hashLinkedNode->next!=NULL) 
      { 
       hashLinkedNode = hashLinkedNode->next; 
      } 
      hashLinkedNode->next = malloc(sizeof(table)); 
      hashLinkedNode->next->name = (char *)malloc((strlen(str) + 1) * sizeof(char*)); 
      strcpy(hashLinkedNode->next->name,str); 
      hashLinkedNode->next->data = val; 
      hashLinkedNode->next->next = NULL; 
     } 
    } 
    fclose(inFile); 
    return 0; 
} 

void printList(BookNode *hd) 
{ 
    for (; hd != NULL; hd = hd->next) 
    { 
     printf("[%s,%d]", hd->name, hd->isbn); 
     if (hd->next) 
      printf(" -> "); 
    } 
    printf("\n"); 
} 

void printHashTable(table **temp) 
{ 
    table *tmp = NULL; 
    int i; 
    for(i = 0; i < SIZE; i++) 
    { 
     tmp = temp[i]; 
     while(tmp) 
     { 
      printf("%s %d",tmp->name, tmp->data); 
      tmp = tmp->next; 
     } 
    } 
} 

int main(int argc, char *argv[]) 
{ 
    if (argc < 2) 
     return -1; 
    table *hashLinked[SIZE]; 
    insert(argv[1],hashLinked); 
    printHashTable(hashLinked); 
    return 0; 
} 

其編譯無警告的,看起來像它可能做你的原意。

相關問題