Ok, so I am having trouble reading in the string names and storing them in my struct array of type student. The age field is an int and the gpa field is a double and those worked fine. Then when I added the name to be read I started running into issues. How can I successfully read in a name and store it in the student.name field? The code below crashes and I am not sure why. I am new in C so please tell me if there is a better way to do this. Thank you in advance. Oh and the name is consecutive letters with no spaces between characters.
typedef struct student{
char *name; int age; double gpa;
}student;
void read(char *filename) {
File *file = fopen(filename,"r");
if(!file) return;
student *students = malloc(sizeof(student)*100);
int num_students = 10; //for example
int i;
for (i=0;i<num_students;i++) {
char *n = malloc(MAXLENGTH);
fscanf(file,"%s %i %lf", n,&students[i].age,&students[i].gpa); //<---runtime error occurs here
strcpy(students[i].name,n);
free(n);
}
/*code here to do stuff with the array*/
free(students);
fclosef(file);
}