Vote count:
0
I'm trying to run a C project for my university assignment and I've hit a seg fault on 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 me 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!
asked 2 mins ago
1 Answer
Vote count:
0
You need to do while (current != NULL) instead of current->next != NULL since the last element in the list will cause segfault.
answered 45 secs ago
Tricky seg fault in C
Aucun commentaire:
Enregistrer un commentaire