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