Okay, I'm very new to C and need an explanation as to why I'm getting this error:
"variable 'newFilm' has initializer but incomplete type"
The task is to create a struct called film. Then pass the data from a .txt file into that struct and create a linked list of structs representing all of the data in the .txt
The problem seems to be that the compiler is missing the point where I allocate memory for the struct newFilm which I believe is being done correctly
Code in main file:
char* t = (char*)malloc(sizeof(char*));
int y;
char* r = (char*)malloc(sizeof(char*));
char* g = (char*)malloc(sizeof(char*));
int rt;
double s;
List* list = newList();
//read pReadFile
char input[256];
//read characters from file being pointed at, and store into input
while( fgets( input, 256, pReadFile )) {
//scan each line with each variable separated by a comma
fscanf(pReadFile,"%s %d %s %s %d %d\n", t,y,r,g,rt,s);
struct Film newFilm = createFilm(t,y,r,g,rt,s); //ERROR OCCURS HERE
addToList(list, newFilm);
}
printList(list, pWriteFile);
Here is the createFilm function from film.c source file:
Film *createFilm(char *title, int year, char *rating,
char *genre, int runtime, double score){
Film *newFilm = (Film*)malloc(sizeof(Film));
// n.b. error checking to be added - to be added
title = (char*)malloc(sizeof(title));
newFilm->title = title;
newFilm->year = year;
rating = (char*)malloc(sizeof(rating));
newFilm->rating = rating;
genre = (char*)malloc(sizeof(genre));
newFilm->genre = genre;
newFilm->runtime = runtime;
newFilm->score = score;
return newFilm;
}
While I don't think there is anything wrong with the addToList function I thought I'd keep it so you have better context (in database.h file):
void addToList(List* list, struct Film* film){
Node *node = (Node*)malloc(sizeof(Node));
//Generates an error message and the program terminates if
//insufficient memory is available.
if (node == NULL){
fprintf(stderr, "Error: Unable to allocate memory in list_add()\n");
exit(EXIT_FAILURE);
}
//appends film to tail of linked list
node->film = film;
node->next = NULL;
if (list->last == NULL){
list->first = list->last = node;
}
else{
list->last = list->last->next = node;
}
}
Thanks in advance :)