2013-10-26 47 views
0

为什么不能我访问指针“细胞”等的阵列?我已经分配了相应的内存,为什么不在这里像一个数组?它就像一个数组,用于基本数据类型的指针。错误:无效类型参数“ - >”(具有“结构节点”)

#include<stdio.h> 
#include<stdlib.h> 
#include<ctype.h> 
#define MAX 10 
struct node 
{ 
    int e; 
    struct node *next; 
}; 

typedef struct node *List; 
typedef struct node *Position; 

struct Hashtable 
{ 
    int Tablesize; 
    List Cells; 
}; 

typedef struct Hashtable *HashT; 

HashT Initialize(int SIZE,HashT H) 
{ 
    int i; 
    H=(HashT)malloc(sizeof(struct Hashtable)); 
    if(H!=NULL) 
    { 
     H->Tablesize=SIZE; 
     printf("\n\t%d",H->Tablesize); 
     H->Cells=(List)malloc(sizeof(struct node)* H->Tablesize); 

它应该不像从这里的数组行事?

 if(H->Cells!=NULL) 
     { 
      for(i=0;i<H->Tablesize;i++) 

以下行是抛出该错误的那些

  { H->Cells[i]->next=NULL; 
       H->Cells[i]->e=i; 
       printf("\n %d",H->Cells[i]->e); 
      } 
     } 
    } 
    else printf("\nError!Out of Space"); 
} 

int main() 
{ 
    HashT H; 
    H=Initialize(10,H); 
    return 0; 
} 

我得到的错误是在标题中错误:invalid type argument of '->' (have 'struct node').

+0

首先,缩进代码。 – Kunal

+6

'H-> Cells - > [i] e'绝对不是正确的语法。 – godel9

+0

然后注意'H-> Cells - > [i] e = i;'不是C语法。 –

回答

0

H->Cells[i]->next 

应be

H->Cells[i].next 

(与e类似)

1

您的代码的正确版本如下所示。总是建议不要在使用typedef时使用指针。

与您的代码唯一的问题除了那个是你的访问方法。 H->cells[i]->next将引发错误。

而且H->cells->[i]e是无效的语法。

#include<stdio.h> 
#include<stdlib.h> 
#include<ctype.h> 
#define MAX 10 
struct node 
{ 
    int e; 
    struct node *next; 
}; 
typedef struct node List; 
typedef struct node Position; 
struct Hashtable 
{ 
    int Tablesize; 
    List *Cells; 
}; 
typedef struct Hashtable HashT; 

HashT Initialize(int SIZE,HashT *H) 
{ 
    int i; 
    H=(HashT*)malloc(sizeof(struct Hashtable)); 
    if(H!=NULL) 
    { 
     H->Tablesize=SIZE; 
     printf("\n\t%d",H->Tablesize); 
     H->Cells=(List*)malloc(sizeof(List)*H->Tablesize); 
    //should it not act like an array from here on? 
     if(H->Cells!=NULL) 
     { 
      for(i=0;i<H->Tablesize;i++) 
    //the following lines are the ones that throw the error 
      { 
       H->Cells[i].next=NULL; 
       H->Cells[i].e=i; 
       printf("\n %d",H->Cells[i].e); 
      } 
     } 
    } 
    else printf("\nError!Out of Space"); 
    return *H; 
} 

int main() 
{ 
    HashT H; 
    H=Initialize(10,&H); //return is not required as already we are passing by address 
    return 0; 
} 

+0

的指针数组,可以像下面这样访问next和e:H-> Cells [i] - > e ??如果不是,为什么不是因为'Cells'是一个指向结构本身的指针? –

+0

不,这是不可能的。 'Cells'的类型为'List *','Cells [i]'的类型为List和NOT List *'。 箭头运算符只能用于指针。 – Sohaib

0

这是没有的typedef一个版本的程序。哪一个更具可读性?

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

struct node { 
    struct node *next; 
    int e; 
    }; 

struct Hashtable { 
    unsigned Tablesize; 
    struct node *Cells; 
    }; 

struct Hashtable *Initialize(unsigned size) 
{ 
    unsigned iii; 
    struct Hashtable *hp; 

    hp = malloc (sizeof *hp); 
    if(!hp) { 
     fprintf(stderr, "Error!Out of Space\n"); 
     return NULL; 
     } 

    hp->Cells = malloc(size * sizeof *hp->Cells); 
    if(!hp->Cells) { 
      hp->Tablesize = 0; 
      return hp; 
      } 

    hp->Tablesize = size; 
    fprintf(stderr, "\t%u\n", hp->Tablesize); 
    for(iii=0; iii < hp->Tablesize; iii++) { 
     hp->Cells[iii].next = NULL; 
     hp->Cells[iii].e = iii; 
     fprintf(stderr, " %u\n", hp->Cells[iii].e); 
     } 
    return hp; 
} 

int main() 
{ 
    struct Hashtable *hashtab; 

    hashtab = Initialize(10); 
    return 0; 
} 

的变化:

  • 除去的typedef;因为它们被混淆
  • 除去从malloc的石膏()不需要和潜在的危险。
  • 将尺寸更改为无符号。尺寸不能为负数
  • 诊断输出应该转到stderr。
  • 几压痕水平可以通过首先做错误的情况下,从功能上错误月初返回来避免。
相关问题