C语言 C 编程:取消引用指向不完整类型错误的指针
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2576554/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C programming: Dereferencing pointer to incomplete type error
提问by confusedKid
I have a struct defined as:
我有一个结构定义为:
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;
...
I'm getting the following error:
我收到以下错误:
error: dereferencing pointer to incomplete type
错误:取消引用指向不完整类型的指针
whenever I try to access the members inside newFile. What am I doing wrong?
每当我尝试访问里面的成员时newFile。我究竟做错了什么?
回答by AnT
You haven't defined struct stasher_fileby your first definition. What you have defined is an namelessstruct type and a variable stasher_fileof that type. Since there's no definition for such type as struct stasher_filein your code, the compiler complains about incomplete type.
您还没有按照struct stasher_file您的第一个定义进行定义。您定义的是一个无名的结构类型和该类型的变量stasher_file。由于struct stasher_file在您的代码中没有对此类类型的定义,因此编译器会抱怨类型不完整。
In order to define struct stasher_file, you should have done it as follows
为了定义struct stasher_file,您应该按如下方式完成
struct stasher_file {
char name[32];
int size;
int start;
int popularity;
};
Note where the stasher_filename is placed in the definition.
请注意stasher_file名称在定义中的位置。
回答by Brian R. Bondy
You are using the pointer newFilewithout allocating space for it.
您正在使用指针newFile而没有为其分配空间。
struct stasher_file *newFile = malloc(sizeof(stasher_file));
Also you should put the struct name at the top. Where you specified stasher_file is to create an instance of that struct.
此外,您应该将结构名称放在顶部。您指定 stasher_file 的位置是创建该结构的实例。
struct stasher_file {
char name[32];
int size;
int start;
int popularity;
};
回答by Dirk
How did you actually define the structure? If
你实际上是如何定义结构的?如果
struct {
char name[32];
int size;
int start;
int popularity;
} stasher_file;
is to be taken as type definition, it's missing a typedef. When written as above, you actually define a variable called stasher_file, whose type is some anonymous struct type.
将被视为类型定义,它缺少一个typedef. 当如上编写时,您实际上定义了一个名为 的变量stasher_file,其类型是某种匿名结构类型。
Try
尝试
typedef struct { ... } stasher_file;
(or, as already mentioned by others):
(或者,正如其他人已经提到的那样):
struct stasher_file { ... };
The latter actually matches your use of the type. The first form would require that you remove the structbefore variable declarations.
后者实际上与您对该类型的使用相匹配。第一种形式要求您删除struct之前的变量声明。
回答by Pete Brumm
the case above is for a new project. I hit upon this error while editing a fork of a well established library.
上面的案例是针对一个新项目的。我在编辑一个完善的库的分支时遇到了这个错误。
the typedef was included in the file I was editing but the struct wasn't.
typedef 包含在我正在编辑的文件中,但结构没有。
The end result being that I was attempting to edit the struct in the wrong place.
最终结果是我试图在错误的位置编辑结构。
If you run into this in a similar way look for other places where the struct is edited and try it there.
如果您以类似的方式遇到这种情况,请寻找编辑结构的其他地方并在那里尝试。
回答by jamesdlin
The reason why you're getting that error is because you've declared your structas:
您收到该错误的原因是因为您已将自己声明struct为:
struct {
char name[32];
int size;
int start;
int popularity;
} stasher_file;
This is not declaring a stasher_filetype. This is declaring an anonymousstructtype and is creating a global instance named stasher_file.
这不是声明stasher_file类型。这声明了一个匿名struct类型并创建了一个名为 的全局实例stasher_file。
What you intended was:
你的意图是:
struct stasher_file {
char name[32];
int size;
int start;
int popularity;
};
But note that while Brian R. Bondy's response wasn't correct about your error message, he's right that you're trying to write into the structwithout having allocated space for it. If you want an array of pointers to struct stasher_filestructures, you'll need to call mallocto allocate space for each one:
但请注意,虽然 Brian R. Bondy 的回答对您的错误消息并不正确,但他说您在尝试写入时struct没有为其分配空间是正确的。如果你想要一个指向struct stasher_file结构的指针数组,你需要调用malloc为每个指针分配空间:
struct stasher_file *newFile = malloc(sizeof *newFile);
if (newFile == NULL) {
/* Failure handling goes here. */
}
strncpy(newFile->name, name, 32);
newFile->size = size;
...
(BTW, be careful when using strncpy; it's not guaranteed to NUL-terminate.)
(顺便说一句,使用时要小心strncpy;不能保证 NUL 终止。)

