c - 错误 : invalid use of undefined type ‘struct Item’

标签 c struct dynamic-allocation

这里是相关的结构。请注意,它们位于同一文件的标题中。

typedef enum {AAA = 0, BBB = 1, CCC = 2, DDD = 3} Subject;

typedef struct item {
    Subject sub;
    int n, h;
    char title[1024];
} Item;

struct Item* Collection = NULL;

在 main 中,我为 Collection 分配了空间:

Collection = malloc(sizeof(Item*));

然后在我的 main 中有一个调用 insert() 函数的 switch 语句该函数如下:

void course_insert() {
    Collection = realloc(Collection,(sizeof(Collection) + sizeof(Iem)));
    printf("What is the subject? (AAA=0, BBB=1, CCC=2, DDD=3)? ");
    scanf("%u", Collection[count].sub);
    printf("What is the number (e.g 20)? ");
    scanf("%d", Collection[count]->n);
    printf("How many hours (e.g. 3)? ");
    scanf("%d", Collection[count]->h);
    printf("What is the name of the item? ");
    scanf("%s", Collection[count]->title);   
    count++;
}

我收到一条错误消息,指出这是对未定义类型“struct Item”的无效使用,但我可以在其他地方正常使用它。谁能找出问题所在?

最佳答案

typedef 语句表示typedef struct item Item。这意味着您定义了 struct itemItem 但没有定义 struct Item。尝试使用 struct itemItem

关于c - 错误 : invalid use of undefined type ‘struct Item’ ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45002942/

相关文章:

c - 具有不同变量类型的共享内存段 C

Python ctypes 指向结构的指针作为标识符,无需成员访问

c - 分配指向结构的指针,但不是实际的结构

c - 随机双释放或无效的next_size

c - 函数采用结构体的 void 指针,并将指向其成员的指针传递给另一个函数

c - 区分设备断开超时

c++ - 从 QMap 中删除一个指针?

c - 带 void 的动态数组内存分配**

c - 矩阵中总和最大的行

c - 在函数中声明在 int main() 中多次调用的变量(在 C 中)