43

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?

1
  • 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). Commented May 6, 2016 at 18:51

6 Answers 6

53

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.

Sign up to request clarification or add additional context in comments.

1 Comment

+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.
13

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

5 Comments

How do I allocate space for it?
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?
@confuseKid: yes you need to allocate space like I gave. Also make sure to free it when done with it.
@confuseKid: I recommend that you accept @AndreyT's answer though, he gives a better explanation for the source of the error.
I believe the actual question asked was "What am I doing wrong?", so this is a more complete answer.
12

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.

Comments

5

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.

Comments

1

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

3 Comments

if you have defined structure as typedef struct { ... } stasher_file; then you can use malloc as stasher_file *newFile = malloc(sizeof (stasher_file);
@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.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.