简体   繁体   中英

Using a malloc for an int array in typedef struct

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

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

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM