News for hackers. Stuff that matters.
Re:List Processing in C by JoachimSchipper on Tuesday December 21, 2010 @06:46AM (#2027545)
The proper way to do this is (simplified from BSD sys/queue.h):

    #define LIST_FOREACH(var, head) \
        for ((var) = (head); (var) != NULL; (var) = (var)->next);
    LIST_FOREACH(list_p, list_head) {
        ...
    }
You may also occasionally want to use (C99) variadic macros (gcc seems to need -std=c99):

    #define LIST_MAP(my_list, f, ...) \
    	for (struct list *n = my_list; n != NULL; n = n->next) \
    		f(__VA_ARGS__, n)
    
    void list_print(const char *format, struct list *n) {
    	printf(format, n->data);
    }
    
    int main(void) {
    	struct list	 n3 = {NULL, 3},
    			 n2 = {&n3,  2},
    			 n1 = {&n2,  1};
    
    	LIST_MAP(&n1, list_print, "%d\n");
    }

There are no comments on this comment yet.