#include <stdio.h> int main() { /***Declaration of non-local variable in 'for' loop***/ for (struct { int i; } s = {0}; s.i < 25; ++s.i) { printf("----------- "); } /**等价方式**/ { struct { int i; } s = {0}; for (; s.i < 25; ++s.i) { printf("----------- "); } } return 0; }
参考自:https://stackoverflow.com/questions/53468223/declare-structure-within-for
这是我在使用一份代码[https://github.com/DavidLeeds/hashmap]的时候发现的问题:
#define __HASHMAP_FOREACH(x, key, data, h) for (HASHMAP_ITER(*(h)) __HASHMAP_UNIQUE(x, it) = hashmap_iter(h, &__HASHMAP_UNIQUE(x, it)); ((key) = hashmap_iter_get_key(&__HASHMAP_UNIQUE(x, it))) && ((data) = hashmap_iter_get_data(&__HASHMAP_UNIQUE(x, it))); hashmap_iter_next(&__HASHMAP_UNIQUE(x, it))) #define __HASHMAP_FOREACH_SAFE(x, key, data, h, temp_ptr) for (HASHMAP_ITER(*(h)) __HASHMAP_UNIQUE(x, it) = hashmap_iter(h, &__HASHMAP_UNIQUE(x, it)); ((temp_ptr) = (void *)((key) = hashmap_iter_get_key(&__HASHMAP_UNIQUE(x, it)))) && ((data) = hashmap_iter_get_data(&__HASHMAP_UNIQUE(x, it))); ((temp_ptr) == (void *)hashmap_iter_get_key(&__HASHMAP_UNIQUE(x, it))) ? hashmap_iter_next(&__HASHMAP_UNIQUE(x, it)) : __HASHMAP_ITER_RESET(&__HASHMAP_UNIQUE(x, it))) #define __HASHMAP_FOREACH_KEY(x, key, h) for (HASHMAP_ITER(*(h)) __HASHMAP_UNIQUE(x, it) = hashmap_iter(h, &__HASHMAP_UNIQUE(x, it)); (key = hashmap_iter_get_key(&__HASHMAP_UNIQUE(x, it))); hashmap_iter_next(&__HASHMAP_UNIQUE(x, it))) #define __HASHMAP_FOREACH_KEY_SAFE(x, key, h, temp_ptr) for (HASHMAP_ITER(*(h)) __HASHMAP_UNIQUE(x, it) = hashmap_iter(h, &__HASHMAP_UNIQUE(x, it)); ((temp_ptr) = (void *)((key) = hashmap_iter_get_key(&__HASHMAP_UNIQUE(x, it)))); ((temp_ptr) == (void *)hashmap_iter_get_key(&__HASHMAP_UNIQUE(x, it))) ? hashmap_iter_next(&__HASHMAP_UNIQUE(x, it)) : __HASHMAP_ITER_RESET(&__HASHMAP_UNIQUE(x, it))) #define __HASHMAP_FOREACH_DATA(x, data, h) for (HASHMAP_ITER(*(h)) __HASHMAP_UNIQUE(x, it) = hashmap_iter(h, &__HASHMAP_UNIQUE(x, it)); (data = hashmap_iter_get_data(&__HASHMAP_UNIQUE(x, it))); hashmap_iter_next(&__HASHMAP_UNIQUE(x, it))) #define __HASHMAP_FOREACH_DATA_SAFE(x, data, h, temp_ptr) for (HASHMAP_ITER(*(h)) __HASHMAP_UNIQUE(x, it) = hashmap_iter(h, &__HASHMAP_UNIQUE(x, it)); ((temp_ptr) = (void *)hashmap_iter_get_key(&__HASHMAP_UNIQUE(x, it))) && ((data) = hashmap_iter_get_data(&__HASHMAP_UNIQUE(x, it))); ((temp_ptr) == (void *)hashmap_iter_get_key(&__HASHMAP_UNIQUE(x, it))) ? hashmap_iter_next(&__HASHMAP_UNIQUE(x, it)) : __HASHMAP_ITER_RESET(&__HASHMAP_UNIQUE(x, it)))
更正:
#define __HASHMAP_FOREACH(x, key, data, h) HASHMAP_ITER(*(h)) __HASHMAP_UNIQUE(x, it) = hashmap_iter(h, &__HASHMAP_UNIQUE(x, it)); for (; ((key) = hashmap_iter_get_key(&__HASHMAP_UNIQUE(x, it))) && ((data) = hashmap_iter_get_data(&__HASHMAP_UNIQUE(x, it))); hashmap_iter_next(&__HASHMAP_UNIQUE(x, it))) #define __HASHMAP_FOREACH_SAFE(x, key, data, h, temp_ptr) HASHMAP_ITER(*(h)) __HASHMAP_UNIQUE(x, it) = hashmap_iter(h, &__HASHMAP_UNIQUE(x, it)); for (; ((temp_ptr) = (void *)((key) = hashmap_iter_get_key(&__HASHMAP_UNIQUE(x, it)))) && ((data) = hashmap_iter_get_data(&__HASHMAP_UNIQUE(x, it))); ((temp_ptr) == (void *)hashmap_iter_get_key(&__HASHMAP_UNIQUE(x, it))) ? hashmap_iter_next(&__HASHMAP_UNIQUE(x, it)) : __HASHMAP_ITER_RESET(&__HASHMAP_UNIQUE(x, it))) #define __HASHMAP_FOREACH_KEY(x, key, h) HASHMAP_ITER(*(h)) __HASHMAP_UNIQUE(x, it) = hashmap_iter(h, &__HASHMAP_UNIQUE(x, it)); for (; (key = hashmap_iter_get_key(&__HASHMAP_UNIQUE(x, it))); hashmap_iter_next(&__HASHMAP_UNIQUE(x, it))) #define __HASHMAP_FOREACH_KEY_SAFE(x, key, h, temp_ptr) HASHMAP_ITER(*(h)) __HASHMAP_UNIQUE(x, it) = hashmap_iter(h, &__HASHMAP_UNIQUE(x, it)); for (; ((temp_ptr) = (void *)((key) = hashmap_iter_get_key(&__HASHMAP_UNIQUE(x, it)))); ((temp_ptr) == (void *)hashmap_iter_get_key(&__HASHMAP_UNIQUE(x, it))) ? hashmap_iter_next(&__HASHMAP_UNIQUE(x, it)) : __HASHMAP_ITER_RESET(&__HASHMAP_UNIQUE(x, it))) #define __HASHMAP_FOREACH_DATA(x, data, h) HASHMAP_ITER(*(h)) __HASHMAP_UNIQUE(x, it) = hashmap_iter(h, &__HASHMAP_UNIQUE(x, it)); for (; (data = hashmap_iter_get_data(&__HASHMAP_UNIQUE(x, it))); hashmap_iter_next(&__HASHMAP_UNIQUE(x, it))) #define __HASHMAP_FOREACH_DATA_SAFE(x, data, h, temp_ptr) HASHMAP_ITER(*(h)) __HASHMAP_UNIQUE(x, it) = hashmap_iter(h, &__HASHMAP_UNIQUE(x, it)); for (; ((temp_ptr) = (void *)hashmap_iter_get_key(&__HASHMAP_UNIQUE(x, it))) && ((data) = hashmap_iter_get_data(&__HASHMAP_UNIQUE(x, it))); ((temp_ptr) == (void *)hashmap_iter_get_key(&__HASHMAP_UNIQUE(x, it))) ? hashmap_iter_next(&__HASHMAP_UNIQUE(x, it)) : __HASHMAP_ITER_RESET(&__HASHMAP_UNIQUE(x, it)))