简体   繁体   中英

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 . What am I doing wrong?

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.

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;
};

Note where the stasher_file name is placed in the definition.

You are using the pointer newFile without allocating space for it.

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.

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 . When written as above, you actually define a variable called stasher_file , whose type is some anonymous struct type.

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.

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.

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 {
 char name[32];
 int  size;
 int  start;
 int  popularity;
} stasher_file;

This is not declaring a stasher_file type. This is declaring an anonymous struct type and is creating a global instance named 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. 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 *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.)

The reason is you did not declare type struct stasher_file , you define a struct variable stasher_file instead.

In C , the declaration of structure:

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

structure-tag is an optional name following the keyword 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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