Hello, I've to build a database of students in a certain school in which each student has a list of her/his passed exams along with the grade gotten.
I have 3 structs and I need an advice on how to carry out memory allocation, eg. when I add a student.
struct exam {
char exam_code[4];
int grade;
};
struct student {
char name[16];
int num_exams;
struct exam **exams;
};
struct school {
int total_students;
struct student **students;
};
int main() {
struct school sc;
sc.total_students = 0;
// now I add ths first student (with no exams)
sc.total_students++;
students = malloc(sizeof(struct student));
strcpy(students[0]->name, "jack");
students[0]->num_exams = 0;
// then the second
students = realloc(students, 2 * sizeof(struct student));
strcpy(students[1]->name, "peter");
students[1]->num_exams = 0;
return 0;
}
Any hint will be highly appreciated...