Declaration of non-local variable in 'for' loop
阿新 • • 發佈:2020-10-12
#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("-----------\n"); } /**等價方式**/ { struct { int i; } s = {0}; for(; s.i < 25; ++s.i) { printf("-----------\n"); } } 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)))