I'm working on an assignment and can't seem to figure out how to properly allocate space for a flexible array member of a struct type in a second structure. The application is as follows:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NUMCOURSES 5
#define LINESIZE 50
typedef struct studentCourse {
char courseName[20];
int courseCredit;
int grade;
} course;
typedef struct studentRecord {
char firstName[20];
char lastName[20];
int ID;
course courseInfo[NUMCOURSES];
} student;
int menu()
{
int choice;
printf("\n**********************************\n");
printf(" 1) Select input file\n");
printf(" 2) Sort records by last name\n");
printf(" 3) Search records by last name\n");
printf(" 4) Search records by GPR\n");
printf(" 5) Quit\n");
printf("**********************************\n\n");
scanf("%d", &choice);
return choice;
}
student parseStudent(char * line)
{
student aStudent;
sscanf(line, "%19s %19s %d %s %d %d %s %d %d %s %d %d %s %d %d %s %d %d",
aStudent.firstName, aStudent.lastName, &aStudent.ID,
aStudent.courseInfo[0].courseName, &aStudent.courseInfo[0].courseCredit, &aStudent.courseInfo[0].grade,
aStudent.courseInfo[1].courseName, &aStudent.courseInfo[1].courseCredit, &aStudent.courseInfo[1].grade,
aStudent.courseInfo[2].courseName, &aStudent.courseInfo[2].courseCredit, &aStudent.courseInfo[2].grade,
aStudent.courseInfo[3].courseName, &aStudent.courseInfo[3].courseCredit, &aStudent.courseInfo[3].grade,
aStudent.courseInfo[4].courseName, &aStudent.courseInfo[4].courseCredit, &aStudent.courseInfo[4].grade);
return aStudent;
}
void getStudentArray(student * arr)
{
char filename[25];
FILE * fp;
int numRecords;
int i;
char line[LINESIZE];
student * ps1;
printf("Enter the input file name: ");
scanf("%s", filename);
if((fp = fopen(filename, "r")) == NULL)
{
printf("File input error.");
exit(1);
}
fgets(line, sizeof(int), fp);
numRecords = atoi(line);
arr = (student *) malloc((sizeof(student) * numRecords) + (numRecords * NUMCOURSES * sizeof(course)));
for( i = 0; i < numRecords; i++)
{
arr[i] = parseStudent(fgets(line, 2 * sizeof(student), fp));
}
}
int main(void)
{
student * studentArray;
int choice;
do
{
choice = menu();
switch(choice)
{
case 1:
printf("Data input.\n");
getStudentArray(studentArray);
break;
case 2:
printf("Sort by last name.\n");
break;
case 3:
printf("Search by last name.\n");
break;
case 4:
printf("Search by GPR.\n");
break;
case 5:
printf("Goodbye.\n");
exit(0);
}
} while(1);
return 0;
}
An example of the input file input.data is
Code:
5
Jeffrey Carouth 371625342 MATH152 4 3 PHYS218 4 4 ENGR111 2 4 HIST105 3 3 CPSC206 4 4
Bob Dillan 873193526 MATH151 4 4 PHYS218 4 4 ENGR111 2 3 KINE199 1 4 POLS206 3 4
Matthew Bennett 943721745 MATH150 4 2 ENGR111 2 2 PHYS218 4 1 HIST105 3 3 LAND111 3 1
Jasen Petersen 624392761 MATH111 3 4 BUSI112 4 3 MGMT209 4 4 HIST109 3 3 MKTG209 4 4
Jordan Brown 926873321 ENGL432 3 4 ENGR482 3 4 CPSC469 3 3 MKTG422 4 4 HIST322 3 4
After it loops through the first element of the student array, I get a segmentation fault I assume because I didn't properly allocate space. I tried to separate the allocation into separate steps (i.e. allocate memory for the student "array" and then for each element of that "array" allocate space for the courseInfor array. That didn't work. Any help is appreciated.