[ILUG] C puzzle
paul at clubi.ie
Tue Jul 6 12:00:53 IST 2004
On Tue, 6 Jul 2004, Philip Reynolds wrote:
> You need to use a temporary variable, to allow the use of free() or
> removal of an element in the list.
Indeed. the problem is trying to stuff it into the for loop so that
macro will still work :)
> #define LIST_LOOP(L, V, N, temp) \
> for ((N) = (L)->head; \
> (N) && ((temp) = (N)->next); \
> (N) = (temp))
> Something like the above.
Nah, that breaks the macro.
However, I've had very useful suggestions from messrs Lyda and
Arlied, which I've to go test out. Something like:
#define LIST_LOOP (list, val, this, next) \
for ( (this) = (list)->head, (next) = (this)->next; \
(this) != NULL; \
(next) = (this)->next, (this) = (next)) \
if ( ((val) = (this)->data) != NULL)
Should do the trick (as discerned from the linux kernel list macros,
which dave pointed me at, so damn obvious once you see it of course).
Lyda actually managed to shove conditionals into the for loop, but in
his defence, because I had insisted that i needed assignment of next
to be conditional, so it's not his fault ;). I havnt tested whether
it's portable yet though (it has to work with a certain compiler
which is a bit stricter than gcc).
> Philip Reynolds | RFC Networks Ltd.
Paul Jakma paul at clubi.ie paul at jakma.org Key ID: 64A2FF6A
warning: do not ever send email to spam at dishone.st
panic: kernel segmentation violation. core dumped (only kidding)
More information about the ILUG