0

为什么我不能像数组一样访问指针“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').

4

3 回答 3

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;
 }

于 2013-10-26T15:07:23.750 回答
0

H->Cells[i]->next

应该

H->Cells[i].next

(同样适用于e。)

于 2013-10-26T14:54:35.177 回答
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。
  • 通过首先执行错误案例,并在错误时从函数中提前返回,可以避免一些缩进级别。
于 2013-10-26T15:56:12.917 回答