next! = NULL) ...">

Wrong error in C

I am trying to run a C project for my university assignment and I hit a seg error at the line "while (current-> next! = NULL) {" in the following code segment:

FILE* f = fileOpen("test.txt");
if (f != NULL){
    functionList = fileReadToMemory(f, &graphParams);//functionList is a pointer to the first value of the linked list it creates
    current = functionList;

    while (current->next != NULL) {
        printf("%d %d %d %s", current->red, current->green, current->blue, current->expression);//Prints value of linked list
        current = current -> next;
    }
}

      

The error that gdb gives is as follows:

Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_INVALID_ADDRESS at address: 0x000000000000003a
0x0000000100000b30 in main () at main.c:23
23          while (current->next != NULL) {

      

What am I doing wrong?

Thanks in advance!

+3


source to share


1 answer


You need to do

while (current != NULL) 

      

instead



current->next != NULL 

      

as the last item in the list will cause a segfault.

+11


source







All Articles