【发布时间】:2021-06-02 15:01:12
【问题描述】:
我遇到了一个关于在 C 中的 typedef 结构中将 malloc 用于数组的问题。 我不知道如何在 typedef 结构中进行动态分配,编译器不报告错误,但程序打开和关闭返回 -1073741819。 在程序中,我正在读取餐厅的评论数量,以获得餐厅本身的平均评论。
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);
}
}
}
我做错了什么? - - 对不起我的英语不好
【问题讨论】:
-
参数列表中的
review arr_rew[LEN]是什么?你还没有定义reviewtypedef。 -
fclose(fp)不应在循环中。 -
你显示的代码是不够的。例如,您谈论“返回”并且您的代码中没有任何内容对应。显示minimal reproducible example。