Writing a function to add a grade to the end of a linked list. There is a linked list of students, each student containing a pointer to a linked list of grades-
typedef struct _grade {
char name[10];
double value;
struct _grade *next;
} Grade;
////////////////////////////////////////////////////////////////////////////////////////
typedef struct _student {
char *lastName;
char *firstName;
Grade *headGradeList;
struct _student *next;
} Student;
When I run my code after compiling, I get a segmentation fault. Im pretty sure it occurs in my if statement with the strcmp. Any suggestions?
// add a grade to the specified student
// 1. Make sure a student by that name exists (so you can add grade to it)
// 2. If the specifed grade already exists, update the grade's value to the new value
// 3. Otherwise, add the grade to the end of the student's grade list (add-at-end)
void addGrade(Student *headStudentList, char last[], char first[], char gradeName[], double value) {
int flag=0;
Student *dummy=headStudentList;
Grade *temp=malloc(sizeof(Grade));
strcpy(temp->name,gradeName);
temp->value=value;
temp->next=NULL;
while(dummy!=NULL){
printf("Here 1");
if(strcmp(dummy->lastName, last)==0 && strcmp(dummy->firstName, first)==0){
flag=1;
if(dummy->headGradeList==NULL){
strcpy(dummy->headGradeList->name, gradeName);
dummy->headGradeList->value=value;
dummy->headGradeList->next=NULL;
}
else{
while(1){
if(dummy->headGradeList->next==NULL){
dummy->headGradeList->next=temp;
break;
}
dummy->headGradeList=dummy->headGradeList->next;
}
}}
dummy=dummy->next;
}
if(flag==0){
printf("ERROR: student does not exist\n");
}
}
structtags.