3 ms·
In C99 and later you can use a similar workaround as JS/TS with designated initialization for functions which take a struct by value or pointer. E.g. for a fun
by flohofwoe 3y ago
In C99 and later you can use a similar workaround as JS/TS with designated initialization for functions which take a struct by value or pointer.
E.g. for a function:
void my_func(const bla_t* bla);
It would look like this:
my_func(&(bla_t){ .x = 123, .y = 542 });
Designators can be in any order and can also be optional (missing items are zero-initialized, so the function should be aware of that and replace them with defaults.
C++20 has a much more restricted designated init feature (basically useless for complex structs), but one nice thing in C++ is that default values can be declared right in the struct declaration.
- gavinray 3y agoThis is one thing I really appreciate about your API design in C/Zig -- the mindfulness of being idiomatic to use by passing (possibly nested) designated initializers.
- rwbt 3y agoI hope the C++ committee will fix their designated init feature implementation (atleast for POD structs) because this single feature makes the code 10x more readable.