4 ms·
Using initializer list would be much more readable without the atrocious macros.
by jxy 4y ago
Using initializer list would be much more readable without the atrocious macros.
- jesse__ 4y agoCare to elaborate, or better yet link to a gist? Initializer lists aren't a thing in C, so I assume you're referring to array initialization : https://en.cppreference.com/w/c/language/array_initialization https://en.cppreference.com/w/c/language/array_initializatio... And with that said, I fail to see how using an initializer would have any effect on the end result.. unless of course you only read the first 150 words. Then your comment, while unnecessary, would make sense.
- david2ndaccount 4y agoInitializer-lists are most definitely a thing in C and are part of the grammar. Check 6.7.9 of your C standard struct Foo { int x, y; }; struct Foo foo = {1, 2}; // ^ this is an initializer list. They also include designators, like: struct Foo foo = {.x=1, .y=2}; The straw man struct initialization at the beginning of the article would be greatly improved with such a construct. Once you are no longer messing with macros, the rest of the article becomes a step too far. I assume the article was for c89, as c89 did not allow non-constant initializers. Any modern C compiler should compile c99 or newer code though.
- jesse__ 4y agoAhh, now I see what you're saying; that does seem true. Sorry for the snarky comment.
- Gibbon1 4y agoYour comment made me look at the article. I saw this line > Can we somehow not require the user to specify the size of the pollset in advance? The answer for gnu99 is yes. // will barf if fed a pointer #define sizeof_array(arr) \ (sizeof(arr) / sizeof((arr)[0]) \ + sizeof(typeof(int[1 - 2 * \ !!__builtin_types_compatible_p(typeof(arr), \ typeof(&arr[0]))])) * 0) typedef struct { int x, y; } Foo; Foo foo[] = { { .x = 10, .y = 20}, { .x = 11, .y = 23} }; foo_cnt = sizeof_array(foo); // foo_cnt == 2 One of the reasons why I think the C standards committee needs to be fired is because sizeof_array should have been made part of the language 20 years ago.