The preface to this question is that I understand that C macros are a touchy subject. They can be achieved a lot of time with a non-macro solution, which is safer and not prone to classical problems, such as additional arguments; so with that in mind, I have a hash table implementation in C with related nodes for collision. I'm sure most of them have seen it a million times, but it looks a bit like this.
typedef struct tnode_t {
char* key; void* value; struct tnode_t* next;
} tnode_t;
typedef struct table_t {
tnode_t** nodes;
unsigned long node_count;
unsigned long iterator;
...
}
I would like to provide an abstract way of iterating through nodes. I considered using a function that takes a pointer to a function and applies the function to each node, but I often find this solution very limited, so I came up with this macro:
for(node=table->nodes[table->iterator=0];\
table->iterator<table->node_count;\
node=node?node->next:table->nodes[++table->iterator])\
if (node)
:
tnode_t* n;
tbleach(mytable, n) {
do_stuff_to(n->key, n->value);
}
, , - , , , , . , , , , . .
** **
Zack Jens, "else" for. , , " ", . , , , , .
for(node=table->nodes[0], unsigned long i=0;\
i<table->node_count;\
node=node?node->next:table->nodes[++i])\
if (!node) {} else
, , ?