0

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

4
  • What is review arr_rew[LEN] in the parameter list? You haven't defined a review typedef. Commented Jun 2, 2021 at 15:04
  • 2
    fclose(fp) should not be in the loop. Commented Jun 2, 2021 at 15:06
  • The code you show is not enough. For example you talk about "returning" and nothing in your code correspond. Show a minimal reproducible example. Commented Jun 2, 2021 at 15:06
  • Please provide a minimal reproducible example. Commented Jun 2, 2021 at 15:07

3 Answers 3

1

What did i do wrong?

You allocated evaluations but wrote arr_rew[i].evaluations unitialised and without indexing by j.

Change:

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

to

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

(or better):

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

and

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

to

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

You also close the file while you are still writing to it, and the use of feof() in this manner is flawed as it is only true after you attempt to read past the end of the file. Instead check the file i/o operations for success and exit the loop if any fail.

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

Sign up to request clarification or add additional context in comments.

Comments

0

You need to assign the result of malloc() to arr_rew[i].evaluations. And then you need to index into that array when reading the evaluation values.

And evaluations should be int*, just like the evaluations member of the reviews struct.

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);
    }
}

Other issues:

1 Comment

Thanks, didn't notice that.
0

Thanks to everybody, the code now finally works with the dynamic allocation of memory :D

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.