c - Segmentation fault 11 on boolean method -
i'm trying use method return 1 true , 0 false, gives me "segmentation fault: 11" if returns false. i've been janking brain trying figure out i'm messing can't seem find anything.
here's code.
first call function
int delete_number; printf("select number remove list?\n"); scanf("%i", &delete_number); if(remove_num(delete_number)) { printf("deleted %i ", delete_number); } else { printf("number %i not found in list", delete_number); }
now function itself
int remove_num(int data) { int result = 0; node *curr_ptr; node *prev_ptr; node *temp_ptr; if(queuer.first != null) { if(queuer.first->data == data) { temp_ptr = queuer.first; queuer.first = queuer.first->next; if(queuer.first == null) { queuer.last = null; } free(temp_ptr); result = 1; } else { prev_ptr = queuer.first; curr_ptr = queuer.first->next; while(curr_ptr->data != data && curr_ptr != null) { prev_ptr = curr_ptr; curr_ptr = prev_ptr->next; } if(curr_ptr != null) { temp_ptr = curr_ptr; prev_ptr->next = temp_ptr->next; free(temp_ptr); result = 1; } } } return result; }
i may have misunderstood seg. fault thought occurs because of how run through list while-loop. said, when user inputs number existing in list, works fine. when number inputted not in list seg.fault occurs.
you need change:
while(curr_ptr->data != data && curr_ptr != null)
to:
while(curr_ptr != null && curr_ptr->data != data)
the tests in &&
executed left-to-right. mustn't dereference curr_ptr
until you've verified it's valid.
Comments
Post a Comment