【问题标题】:Using a malloc for an int array in typedef struct在 typedef struct 中对 int 数组使用 malloc
【发布时间】: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]是什么?你还没有定义review typedef。
  • fclose(fp) 不应在循环中。
  • 你显示的代码是不够的。例如,您谈论“返回”并且您的代码中没有任何内容对应。显示minimal reproducible example

标签: c malloc typedef


【解决方案1】:

我做错了什么?

您分配了evaluations,但写入了arr_rew[i].evaluations,未通过j 进行索引。

变化:

reviews* evaluations=malloc(arr_rew[i].number_of_evaluations*sizeof(int));

arr_rew[i].evaluations = malloc( arr_rew[i].number_of_evaluations * sizeof(int) ) ;

(或更好):

arr_rew[i].evaluations = malloc( arr_rew[i].number_of_evaluations * 
                                 sizeof(*arr_rew[i].evaluations) ) ;

fscanf(fp, "%d", arr_rew[i].evaluations) ;

fscanf( fp, "%d", arr_rew[i].evaluations[j] ) ;

您还在写入文件时也关闭了该文件,并且以这种方式使用feof() 是有缺陷的,因为它只有在您尝试读取文件末尾之后才是正确的文件。而是检查文件 i/o 操作是否成功,如果失败则退出循环。

    int input_check = 1 ;
    for( int i = 0; input_check != 0; i++ )
    {
        input_check = fscanf(fp, "%d", arr_rew[i].number_of_evaluations);

        if( input_check != 0 )
        {
            arr_rew[i].evaluations = malloc( arr_rew[i].number_of_evaluations * 
                                             sizeof(*arr_rew[i].evaluations) ) ;

            for( int j = 0; input_check != 0; j < arr_rew[i].number_of_evaluations; j++ )
            {
                input_check = fscanf( fp, "%d", arr_rew[i].evaluations[j] ) ;
            }

        }
    }
    fclose( fp ) ;

【讨论】:

    【解决方案2】:

    您需要将malloc() 的结果分配给arr_rew[i].evaluations。然后在读取评估值时需要对该数组进行索引。

    evaluations 应该是int*,就像reviews 结构的evaluations 成员一样。

    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;;i++)
            {
                if (fscanf(fp, "%d", arr_rew[i].number_of_evaluations) != 1) {
                    break;
                }
                int* 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", &evaluations[j]);
                }
                arr_rew[i].evaluations = evaluations;
            }
            fclose(fp);
        }
    }
    

    其他问题:

    【讨论】:

    • 谢谢,没注意到。
    【解决方案3】:

    感谢大家,代码现在终于可以动态分配内存了:D

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多