简体   繁体   English

C 编程:取消引用指向不完整类型错误的指针

[英]C programming: Dereferencing pointer to incomplete type error

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 .每当我尝试访问newFile的成员时。 What am I doing wrong?我究竟做错了什么?

You haven't defined struct stasher_file by your first definition. 您还没有通过第一个定义定义struct stasher_file What you have defined is an nameless struct type and a variable stasher_file of that type. 您定义的是无名结构类型和该类型的变量stasher_file 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 ,您应该按如下方式完成

struct stasher_file {
 char name[32];
 int  size;
 int  start;
 int  popularity;
};

Note where the stasher_file name is placed in the definition. 注意stasher_file名称放在定义中的位置。

You are using the pointer newFile without 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;
};

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 . 将被视为类型定义,它缺少typedef When written as above, you actually define a variable called stasher_file , whose type is some anonymous struct type. 如上所述,您实际上定义了一个名为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 struct before variable declarations. 第一种形式要求您在变量声明之前删除struct

the case above is for a new project. 上面的案例是针对一个新项目。 I hit upon this error while editing a fork of a well established library. 我在编辑一个完善的库的fork时遇到了这个错误。

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. 如果您以类似的方式遇到此问题,请查找编辑结构的其他位置并在那里进行尝试。

The reason why you're getting that error is because you've declared your struct as: 您收到该错误的原因是您已将struct声明为:

struct {
 char name[32];
 int  size;
 int  start;
 int  popularity;
} stasher_file;

This is not declaring a stasher_file type. 这不是声明stasher_file类型。 This is declaring an anonymous struct type and is creating a global instance named 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 struct without having allocated space for it. 但请注意,虽然Brian R. Bondy对您的错误消息的回答不正确,但他是正确的,您正试图在没有为其分配空间的情况下写入struct If you want an array of pointers to struct stasher_file structures, you'll need to call malloc to allocate space for each one: 如果你想要一个指向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终止。)

The reason is you did not declare type struct stasher_file , you define a struct variable stasher_file instead.原因是您没有声明类型struct stasher_file ,而是定义了一个 struct 变量stasher_file

In C , the declaration of structure:C ,结构的声明:

    struct structure-tag {
        member1
        member2
        ...
    };

structure-tag is an optional name following the keyword struct . structure-tag是跟在关键字struct之后的可选名称。 After declaration, you can define a variable:声明后,您可以定义一个变量:

    struct structure-tag var1, *var2;

Also, you can do both declaration and definition like:此外,您可以同时进行声明和定义,例如:

    struct structure-tag {
        member1
        member2
        ...
    } var1, *var2;

So in your case, you could try this:所以在你的情况下,你可以试试这个:

struct stasher_file {
 char name[32];
 int  size;
 int  start;
 int  popularity;
} *files[TOTAL_STORAGE_SIZE];

struct stasher_file *newFile = malloc(sizeof(struct stasher_file));

... other code ...

That's all.就这样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM