I got "Process returned -1073741819 (0xC0000005)" while executing a program that dealing with linked lists in C

-2

I was working on a project for school, this code i attached should read courses from the file and add it to linked list ,i added to main function code to print the list to make sure it have been read and stored correctly. but the program didnt work and terminated in middle of execution with the message "Process terminated with status -1073741819" and returned -1073741819 (0xC0000005). I spent two days looking for the problem and I think it is related to pointers but i cant dictate the actual problem. The due date for the project is soon and I am afraid that I will not figure what the problem is. note: I used code::blocks and I tried to run it on an online compiler . it didnt work either but the process terminated message was different .

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define StringLength 30 
#define LineLength 100 

struct course;
struct student;

typedef struct course *course;
typedef struct student *student;
typedef struct course *registeredcourse;
typedef struct course *toberegisteredcourse;
typedef struct student *registeredstudent;
typedef char string[StringLength];

struct course
{
string Name;
string Id;
string Year;
int StartTime;
int FinishTime;
int MaxStudents;
int NumberOfRegisteredStudents;
student RegisteredStudents;
course Next;
};

struct student
{
string Name;
string ID;
int NumberOfRegisteredCourses;
course ToBeRegisteredCourse;
course RegisteredCourse;
student Next;
};

course CoursesHead;
student StudentsHead;

void createheads()
{
course CoursesHead= (course)malloc(sizeof(struct course));
CoursesHead->Next=NULL;
student StudentsHead=(student)malloc(sizeof(struct student));
StudentsHead->Next=NULL;
}
void readcourses(string CoursesFile) 
{
 FILE* Courses= fopen(CoursesFile,"r");
string line;
while(fgets(line,LineLength,Courses)!= NULL)
{
    course node= (course)malloc(sizeof(struct course));
    strcpy(node->Name,strtok(line,"#"));
    strcpy(node->Id,strtok(NULL,"#"));
    strcpy(node->Year,strtok(NULL,"#"));
    int hours=atoi(strtok(NULL,":"));
    int minutes=atoi(strtok(NULL,"#"));
    node->StartTime=hours*60+minutes;
    hours=atoi(strtok(NULL,":"));
    minutes=atoi(strtok(NULL,"#"));
    node->FinishTime =hours*60+minutes;
    node->MaxStudents=atoi(strtok(NULL,"#"));
    node->NumberOfRegisteredStudents=0;
    node->RegisteredStudents=(student)malloc(sizeof(struct student));
    node->RegisteredStudents->Next=NULL;
    course p=CoursesHead;
    while(p->Next!=NULL&&strcmp(p->Next->Year,node->Year)>0)
    {
        p=p->Next;//here
    }
    course temp=p->Next;//here
    p->Next=node;
    node->Next=temp;
}
fclose(Courses);
}


int main()
{
createheads();
readcourses("courses.txt");
course p = CoursesHead->Next;
while(p!=NULL)
{
    printf("%s %s %s %d %d %d\n",p->Name,p->Id,p->Year,p->StartTime,p->FinishTime,p->MaxStudents);
    p=p->Next;//here
}
return 0;
}
c
pointers
struct
linked-list
asked on Stack Overflow Nov 16, 2018 by Hashem

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0