gpt4 book ai didi

c - "Dereferencing pointer to incomplete type", typedefs 没问题

转载 作者:太空宇宙 更新时间:2023-11-04 08:10:15 25 4
gpt4 key购买 nike

我正在尝试实现一个哈希表。我收到上述错误,但 AFAIK 我已经正确声明了我所有的结构/typedef。 (我还在学C,sorry)错误发生在我的 while 循环中的 test.c 中,我在其中放置了//ERROR。怎么了?

测试.c

#include "mentry.h"
#include "mlist.h"

int main() {
FILE *fPtr = fopen("S.txt", "r");
MList *ml = ml_create();

MEntry *m2;
int i = 1;
while ((m2 = me_get(fPtr)) != NULL) {
unsigned long int hash = me_hash(m2, ml->numbuckets); // ERROR HERE
i++;
}

return 0;
}

mlist.c

#include "mentry.h"
#include "mlist.h"

#define NUMBUCKETS 3
#define BUCKETSIZE 5

typedef struct bucket {
int size; // number of elements in this bucket
MEntry *entries; // array of MEntries
} Bucket;

typedef struct mlist {
int numbuckets; // number of buckets
Bucket **buckets; // an array of Bucket pointers
} MList;

/* ml_create - creates a new mailing list
- returns pointer to start of mailing list*/
MList *ml_create(void) {
MList *ml = malloc(sizeof(MList));

ml->numbuckets = NUMBUCKETS; // random initial number
// will increment once a bucket is full

// init Buckets
int b;
for (b=0; b<ml->numbuckets; b++) {
Bucket *bk = malloc(sizeof(Bucket));
bk->size = 0;
bk->entries = calloc(BUCKETSIZE, sizeof(MEntry));
ml->buckets[b] = bk;
}

return ml;
}

mlist.h

#ifndef _MLIST_H_
#define _MLIST_H_

#include "mentry.h"

typedef struct mlist MList;

/* ml_create - creates a new mailing list
- returns pointer to start of mailing list*/
MList *ml_create(void);

#endif /* _MLIST_H_ */

最佳答案

当您尝试访问 main 中的 ml->numbuckets 时,它所知道的关于 m1 类型的所有信息是:

typedef struct mlist MList;

它知道 MList 是一种结构类型,但它对其中的内容一无所知。

你需要把完整的定义放在mlist.h中:

#ifndef _MLIST_H_
#define _MLIST_H_

#include "mentry.h"

typedef struct mlist {
int numbuckets; // number of buckets
Bucket **buckets; // an array of Bucket pointers
} MList;

/* ml_create - creates a new mailing list
- returns pointer to start of mailing list*/
MList *ml_create(void);

#endif /* _MLIST_H_ */

关于c - "Dereferencing pointer to incomplete type", typedefs 没问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40066527/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com