2 ms·
Initializer-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 f
by david2ndaccount 4y ago
Initializer-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.