2012-03-03 75 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; 
} 

其编译无警告的,看起来像它可能做你的原意。

相关问题