为什么我不能像数组一样访问指针“ Cells”?我已经分配了适当的内存,为什么它在这里不像一个数组呢?它像基本数据类型的指针的数组一样工作。

#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').

最佳答案

下面给出了正确的代码版本。始终建议在使用typedef时不要使用指针。

除此以外,您代码的唯一问题是访问方法。
H->cells[i]->next将引发错误。

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

ojit_pre

关于c - 错误:“->”的类型参数无效(具有“结构节点”),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19607706/

10-11 22:23
查看更多