C 编程:取消引用指向不完整类型错误的指针 [英] C programming: Dereferencing pointer to incomplete type error
问题描述
我有一个结构定义为:
struct {
char name[32];
int size;
int start;
int popularity;
} stasher_file;
以及指向这些结构的指针数组:
and an array of pointers to those structs:
struct stasher_file *files[TOTAL_STORAGE_SIZE];
在我的代码中,我创建了一个指向结构的指针并设置其成员,并将其添加到数组中:
In my code, I'm making a pointer to the struct and setting its members, and adding it to the array:
...
struct stasher_file *newFile;
strncpy(newFile->name, name, 32);
newFile->size = size;
newFile->start = first_free;
newFile->popularity = 0;
files[num_files] = newFile;
...
我收到以下错误:
错误:解引用指向不完整类型的指针
error: dereferencing pointer to incomplete type
每当我尝试访问 newFile
中的成员时.我做错了什么?
whenever I try to access the members inside newFile
. What am I doing wrong?
推荐答案
您还没有通过您的第一个定义定义 struct stasher_file
.您定义的是一个 nameless 结构类型和该类型的变量 stasher_file
.由于您的代码中没有像 struct stasher_file
这样的类型的定义,编译器会抱怨类型不完整.
You haven't defined struct stasher_file
by your first definition. What you have defined is an nameless struct type and a variable stasher_file
of that type. Since there's no definition for such type as struct stasher_file
in your code, the compiler complains about incomplete type.
为了定义struct stasher_file
,你应该这样做
In order to define struct stasher_file
, you should have done it as follows
struct stasher_file {
char name[32];
int size;
int start;
int popularity;
};
注意 stasher_file
名称在定义中的位置.
Note where the stasher_file
name is placed in the definition.
这篇关于C 编程:取消引用指向不完整类型错误的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!