[ILUG] C puzzle

Philip Reynolds philip.reynolds at rfc-networks.ie
Tue Jul 6 11:40:45 IST 2004


Paul Jakma <paul at clubi.ie> 67 lines of wisdom included:
> list list;
> listmember this;
> listmember next;
> 
> for (this = list->head;
>      ((this != NULL) ? next = this->next : next = this);
>      this = next)
> 
> Which is not (strictly) allowed.
> 
> Essentially, I have a body of code that uses a macro defined as:
> 
> #define LIST_LOOP(L,V,N) \
>   for ((N) = (L)->head; (N); (N) = (N)->next)
>    if (((V) = (N)->data) != NULL)


You need to use a temporary variable, to allow the use of free() or
removal of an element in the list.

#define LIST_LOOP(L, V, N, temp) \
	for ((N) = (L)->head; \
		(N) && ((temp) = (N)->next); \
		(N) = (temp))

Something like the above.
-- 
Philip Reynolds                      | RFC Networks Ltd.
philip.reynolds at rfc-networks.ie      | +353 (0)1 8832063
http://people.rfc-networks.ie/~phil/ | www.rfc-networks.ie



More information about the ILUG mailing list