0

I am trying to insert data to the end of the list, but it's not working: an exception occurs at runtime.

struct gradeNode *newNode = (struct gradeNode*)malloc(sizeof(struct gradeNode));
assert(newNode != NULL);

strcpy(newNode->courseName, courseName); // copying the course name
newNode->next = NULL;

struct gradeNode *temp = students[i].gradelist->head; // a temp

// here is the problem: the debugger says ecxeption, can't access memory 
while (temp->next != NULL) 
{
    temp = temp->next; // I can't get to here
    temp->next = newNode;
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
loay
  • 91
  • 1
  • 8
  • 2
    [Please see this discussion on why not to cast the return value of malloc() and family in C..](https://stackoverflow.com/q/605845/2173917) – Sourav Ghosh Nov 24 '17 at 10:56
  • 1
    Is `students[i].gradelist->head` properly initialized? When do this happen? Is `temp` a null or otherwise invalid pointer? Please try to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve) and show us. And please take some time to [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask). – Some programmer dude Nov 24 '17 at 10:58
  • 1
    @loay It seems that temp is equal to NULL. – Vlad from Moscow Nov 24 '17 at 10:59
  • On an unrelated note, `assert` is not a function but a *macro*. A macro that doesn't do anything in "release" builds. Don't use it for proper error checking and validation. – Some programmer dude Nov 24 '17 at 10:59
  • @loay And ignore the reference about castings. There is an answer of a very weak programmer. . – Vlad from Moscow Nov 24 '17 at 11:01
  • How did you think this mess was well written enough to post? – underscore_d Nov 24 '17 at 11:18
  • Please post the code for how you initialize `students[i].gradelist->head` – lockcmpxchg8b Nov 24 '17 at 11:20

1 Answers1

0

In this code snippet

    struct gradeNode *temp = students[i].gradelist->head;//a temp
    while (temp->next != NULL)//

either the data member head is not properly initialized or is equal to NULL. In the both cases the expression temp->next results in undefined behavior.

And this loop (after you edited your code)

while (temp->next != NULL) 
{
    temp = temp->next; // I can't get to here
    temp->next = newNode;
}

does not make sense. It seems you mean

while (temp->next != NULL) 
{
    temp = temp->next; // I can't get to here
}
temp->next = newNode;

In any case the used approach is wrong.

Instead try the following

    struct gradeNode **temp = &students[i].gradelist->head;//a temp
    while ( *temp ) temp = &( *temp )->next;

    *temp = newNode;

Take into account that it is logically inconsistent to have a one-sided singly-linked list and to try to append data to the end of the list. If you want to append data to a singly-linked list to its end then the list should be defined as a two-sided linked list.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • iam sorry iam new to c so my code is a bit missy my code works on codeblocks but it dosn`t work on visualstudio the problem is here ` struct gradeNode *temp = students[i].gradelist->head; while (temp->next != NULL) {` ** struct Stud { int id; float gradeAverage; float incomeAverage; struct gradeList *gradelist; struct incomeList *incomelist; }; typedef struct Stud Students; struct gradeNode { char courseName[20]; int grade; struct gradeNode *next; struct gradeNode *prev; };** – loay Nov 26 '17 at 08:25
  • @loay I showed in my answer how the function should be implemented. – Vlad from Moscow Nov 26 '17 at 09:43