【发布时间】:2021-11-25 21:30:09
【问题描述】:
我正在尝试在头文件中的结构中输入一个值。该值在主文件中给出,并作为参数传递给函数。但是我收到一条错误消息,上面写着error: dereferencing pointer to incomplete type ‘struct Array’ array->size = size; 编译器正在讨论array->size = size; 行中的箭头我在下面提供了我的文件。
Header.h:
#include <stdio.h>
//Include libraries used
struct Array {
unsigned int size;
unsigned int cap;
int data;
};
//Declare struct Array
struct Array *newArray(unsigned int size, unsigned int cap);
function.c:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
struct Array *newArray(unsigned int size, unsigned int capacity ) {
struct Array *array;
array->size = size;
}
我的 main.c 文件已包含头文件,因此我的 function.c 文件中没有它。它还调用函数 newArray(5, 20),将 size 的值设为 5。如何修复此错误,以便结构中的 size 等于 5?谢谢,非常感谢您的帮助。
【问题讨论】:
-
您需要在尝试取消引用之前设置
array。这是一个未定义的指针值。 -
此外,
struct Array没有成员width。不要马虎,还要发布您正在尝试的确切代码,不要向我们显示不适用于您发布的代码的错误。 -
@Cheatah 我修复了代码,很抱歉我输入的信息有误。
标签: c struct declaration definition incomplete-type