All pastes #988725 Raw Edit

Someone

public c v1 · immutable
#988725 ·published 2008-04-17 20:45 UTC
rendered paste body
Assume you are trying to traverse through some container. Callingget_next_element() with a NULL argument will get you the first itemin the list.Broken:struct element *x = NULL, *y = NULL;for (x = get_next_element(y); x; y = x, x = get_next_element(y)) {    /* Do some processing */    if (some_condition) {        /* Note that x is non-NULL here */        continue;    }}Why it's broken: Even though x is non-NULL at the end of the for loopget_next_element is called with y still set NULL.Works:for (x = get_next_pointer(y); x; x = get_next_pointer(y)) {    /* Do some processing */    if (some_condition) {        /* Note that x is non-NULL here */        y = x;        continue;    }}