【发布时间】:2021-10-03 21:22:37
【问题描述】:
我有一个结构,位于我的头文件中,我想将它的成员设置为我在 main() 函数中拥有的值,即“size”和“cap”。我得到以下信息:error: expected identifier or ‘(’ before ‘->’ token struct Array->size = size; 我也得到与“cap”行相同的错误。
我已经提供了我的头文件、结构所在的位置以及我的函数定义文件。
头文件:
#include <stdio.h>
struct Array {
unsigned int size;
unsigned int cap;
int data;
};
struct Array *newArray(unsigned int size, unsigned int cap); //Prototype
函数定义文件:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
struct Array *newArray(unsigned int size, unsigned int cap) {
struct Array->size = size;
struct Array->cap = cap;
}
我故意不将我的头文件包含在我的函数定义文件中,因为我将它包含在我的主文件中。包含两次 header.h 会给我更多错误/警告。
有人可以帮忙吗?谢谢
【问题讨论】:
-
struct Array是一种类型。您不能将值存储在类型中。首先创建该类型的实例(malloc 一些空间)。 -
struct Array是类型的名称,而不是您可以使用->运算符的指针变量的名称。您需要先为struct Array分配内存,然后使用指向该内存的指针。 -
你不是
#include-ing你自己的头文件,所以结构的定义(甚至Array存在的类型)是未知的......它是#included在主文件中是无关紧要的。