i was trying to make the linked list with the following code typedef of string is char*
void insert_at_front(CourseList* self, String *course) {//inserting to the front
void insert_at_front(CourseList* self, String *course) {//inserting to the front
CourseNodePtr new = malloc(sizeof * new);
new->course=malloc(sizeof course+1);
strncpy_s(new->course, sizeof course+1, course, sizeof course+1);
new->students = new_bst();
new->next = self->head;
self->head = new;
}
how ever i got error Exception thrown at 0x66C5FF80 (ucrtbased.dll) in Project.exe: 0xC0000005: Access violation reading location 0xCCCCCCCC when i am trying to enter any character or string in course. Just don't know what is wrong with the code.
update: i have edited the code to this,it runs but nothing stored in course if i type in numbers. If i type in any characters, it loops and loops again.
void insert_at_front(CourseList* self, String* course) {//inserting to the front
CourseNodePtr new_node;
if (!(new_node = malloc(sizeof * new_node))) {
perror("malloc");
exit(1);
if (!(new_node->course = malloc(strlen(course) + 1))) {
perror("malloc");
exit(1);
}
strncpy_s(new_node->course, strlen(course) + 1, course, strlen(course) + 1);
new_node->students = new_bst();
new_node->next = self->head;
self->head = new_node;
}
User contributions licensed under CC BY-SA 3.0