C programming: Dereferencing pointer to incomplete type error

225,756

Solution 1

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.

Solution 2

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

Solution 3

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.

Solution 4

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.

Solution 5

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.)

Share:
225,756

Related videos on Youtube

confusedKid
Author by

confusedKid

I do not make sense.

Updated on December 09, 2021

Comments

  • confusedKid
    confusedKid over 2 years

    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?

    • confusedKid
      confusedKid about 14 years
      Thanks everyone for the help :)
    • ady
      ady almost 8 years
      By the way, I had the same error, but the problem was that I did not include a specific header file (in a big project).
  • confusedKid
    confusedKid about 14 years
    How do I allocate space for it?
  • Dirk
    Dirk about 14 years
    +1 Faster than me, and using struct stasher_file instead of a typedef is consistent with the OP's use of the type in the example.
  • confusedKid
    confusedKid about 14 years
    I didn't allocate space for the newFile, but changed the definition of stasher_file to be like yours, and the error didn't come up. Do I still need to allocate space?
  • Brian R. Bondy
    Brian R. Bondy about 14 years
    @confuseKid: yes you need to allocate space like I gave. Also make sure to free it when done with it.
  • Brian R. Bondy
    Brian R. Bondy about 14 years
    @confuseKid: I recommend that you accept @AndreyT's answer though, he gives a better explanation for the source of the error.
  • Marconius
    Marconius about 11 years
    I believe the actual question asked was "What am I doing wrong?", so this is a more complete answer.
  • katta
    katta about 11 years
    if you have defined structure as typedef struct { ... } stasher_file; then you can use malloc as stasher_file *newFile = malloc(sizeof (stasher_file);
  • jamesdlin
    jamesdlin about 11 years
    @katta Yes, but many people consider it to be a better practice to do T* p = malloc(sizeof *p) instead. That way if the type of p ever changes, you have to update only its declaration and not the malloc sites. Forgetting to update the malloc sites would silently allocate the wrong amount of memory, potentially leading to buffer overflows.
  • jamesdlin
    jamesdlin about 11 years