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_fileby your first definition. What you have defined is an nameless struct type and a variablestasher_fileof that type. Since there’s no definition for such type asstruct stasher_filein your code, the compiler complains about incomplete type.In order to define
struct stasher_file, you should have done it as followsNote where the
stasher_filename is placed in the definition.