i'm facing a problem regarding the use of malloc for an array in a typedef struct in C. I don't know how to make the allocation dynamic in a typedef struct, the compiler does not report errors but the program opens and closes returning -1073741819. In the program i'm reading the number of reviews of a restaurant in order to get a review average of the restaurant itself.
typedef struct{
int number_of_evaluations;
int *evaluations;
}reviews;
reviews arr_rew[LEN]; //Where LEN=200
void charge_review(review arr_rew[LEN]){
FILE *fp;
fp = fopen("recensioni.txt", "r");
if(fp == NULL) {
puts("================\nFile didn't opened\n================");
}
else
{
for(i=0;!feof(fp);i++)
{
fscanf(fp, "%d", arr_rew[i].number_of_evaluations);
reviews* evaluations=malloc(arr_rew[i].number_of_evaluations*sizeof(int));
for(int j=0;j<arr_rew[i].number_of_evaluations;j++)
{
fscanf(fp, "%d", arr_rew[i].evaluations);
}
fclose(fp);
}
}
}
What did i do wrong? - - Sorry for my bad english