3

I need a function that reads grades (integers) from from file and returns a dynamically allocated array in which they are stored.

This is what I have tried:

int *readGrades() {
int *grades;
int x;
scanf("%d", &x);
grades = malloc(x * sizeof(int));
return 0;
}

However I don't get anything when I run the code. The grades are stored in file called 1.in:

29
6 3 8 6 7 4 8 9 2 10 4 9 5 7 4 8 6 7 2 10 4 1 8 3 6 3 6 9 4

and I run my program using: ./a.out < 1.in

Can anyone tell me what I did wrong?

LihO
  • 41,190
  • 11
  • 99
  • 167
user2831017
  • 35
  • 1
  • 1
  • 5
  • 1
    The code you show allocates an array then leaks it (by returning `0` rather than `grades`). You haven't shown any code that'd try reading values from file. – simonc Sep 30 '13 at 11:38
  • Also, is the program supposed to work with input redirection? How are supposed to read the file? – StoryTeller - Unslander Monica Sep 30 '13 at 11:39
  • `malloc()` returns the pointer to allocated memory having garbage values, you yourself have to assign the values to allocated memory – 0xF1 Sep 30 '13 at 11:39
  • i don't get anything when i reading !!! make the question proper. Do you want to return array from function? – sujin Sep 30 '13 at 11:39
  • I'm sorry if it's unclear: I want a function readGrades that reads the grades from the input(file) and that return a dynamically allocated array in which they are stored – user2831017 Sep 30 '13 at 11:41
  • "*I want a function readGrades that ...*" so please code it. – alk Sep 30 '13 at 11:50
  • Please also show the actual code that calls your function. – SvenS Sep 30 '13 at 11:56
  • @alk: The OP hasn't just asked for a code, but he actually showed his attempt as well. – LihO Sep 30 '13 at 12:18

2 Answers2

5

Problem: The following code:

int *readGrades() {
    int *grades;
    int x;
    scanf("%d", &x);
    grades = malloc(x * sizeof(int));
    return 0;
}

reads 1 int from the standard input, THEN it allocates an array of ints and it returns 0 which zero-initializes caller's pointer when used like this:

int* grades = readGrades();

Solution: Apart from reading the count of grades, the function should read the grades as well. The array should be initialized BEFORE the reading and the actual reading of grades should be done in a loop, which would initialize array's elements. At the end, a pointer to the first element should be returned:

int *readGrades(int count) {
    int *grades = malloc(count * sizeof(int));
    for (i = 0; i < count; ++i) {
        scanf("%d", &grades[i]);
    }
    return grades;                // <-- equivalent to return &grades[0];
}
...
int count;
scanf("%d", &count);              // <-- so that caller knows the count of grades
int *grades = readGrades(count);  
LihO
  • 41,190
  • 11
  • 99
  • 167
3

Hopefully, you are looking for the following program. This reads your grades.txt, creates the memory and finally frees up. I have tested the following program, and it works fine.

#include "stdio.h"


int main(int argc, char *argv[])
{
  FILE *fp;
  int temp;
  int *grades = NULL;
  int count = 1;
  int index;

  fp = fopen("grades.txt","rb+");

  while( fscanf(fp,"%d",&temp) != EOF )

  {


    if( grades == NULL )

     {

       grades = malloc(sizeof(temp));
       *grades = temp;

       printf("The grade is %d\r\n",temp);
     }

    else
    {
       printf("The grade is realloc %d\r\n",temp);
       count++;
       grades = realloc(grades,sizeof(grades)*count);
       index = count -1;
       *(grades+index) = temp;
       //printf("the index is %d\r\n",index);

    }  

  }   


   /** lets print the data now **/

   temp = 0;

    while( index >= 0 )
    {

        printf("the read value is %d\r\n",*(grades+temp));
        index--;
        temp ++;

    }

    fclose(fp);

    free(grades);
    grades = NULL;


}
dexterous
  • 6,422
  • 12
  • 51
  • 99