4 ms·
Named arguments can be done by passing a struct as the only parameter to a function, without the complicated macros. Missing arguments become 0, so you get opt
by nitrogen 12y ago
Named arguments can be done by passing a struct as the only parameter to a function, without the complicated macros. Missing arguments become 0, so you get optional parameters for free.
func((struct somestruct){.x = 0, .y = 1});
- dsego 12y agoWhat's the point in that? Then you have to write all your functions to accept structs instead of natural arguments.
- ygra 12y agohttp://c2.com/cgi/wiki?ParameterObject http://c2.com/cgi/wiki?ParameterObject Sometimes it's a good way to go.
- simgidacav 12y ago...agreed, what's the point? Besides, a lot of includes are already a mess of #defines, do we really need another bunch of them?
- eliteraspberrie 12y agoThis (compound literals) also has the advantage of being strictly valid C, since compound statements are a GNU extension.
- micampe 12y agoI find your statement puzzling. A GNU extension is by definition not “strictly valid C”, compound literals are valid C because they are defined in C99.
- peaton 12y agoWow, that's really interesting. I had no idea this was possible. Do you know of any links to tutorials or blog posts on this? I'd love to see a more detailed example.
- micampe 12y agoA value of 0 is not the same thing as a missing value.
- mpyne 12y agoThat's true, but the parent comment never claimed that, only that missing arguments are assigned the value 0 in the resultant struct.
- CountHackulus 12y agoAnd that's almost exactly what the macro expands to. It's just some nice syntactic sugar on top.
- nitrogen 12y agoThe function declaration with the macro is a bit awkward, IMO. I don't mean to downplay the awesomeness of the macro approach, though; FOR_EACH and putting __VA_ARGS__ at the beginning of the macro argument list isn't something I had considered before.
- codehero 12y agoThat is really cool. Combine this with struct definitions in function bodies and I see a great teamup with variadic functions. Take a theoretical struct_printf, which prints out non promoted data with struct packing (not variadic stack packing, which requires type promotion) /* Only uses va_start and va_arg to*/ void struct_printf(const char* format, ...){ ... } void print_foo(){ struct foo_t{ float f; uint16_t u2; uint8_t x; } input; struct_printf("No type promotion here: %f %hu %hhx\n", (struct foo_t){ .f = 0.1, .u2 = 1000, .x = 0xF2}); } Now we can actually pass what we mean to pass without the compiler changing it. NOTE: After I wrote this I'm not actually sure what va_args does with a struct or how to get a pointer to the first member...
- nitrogen 12y agoNOTE: After I wrote this I'm not actually sure what va_args does with a struct or how to get a pointer to the first member... I think you would still need to know the exact type inside of struct_printf(), unless you knew exactly how the compiler would pack a struct onto the stack or registers, and that it was exactly the same as individual parameters.
- Peaker 12y agoNote, %hu and %hhx are not portable ways to print uint16_t and uint8_t. Instead, you're supposed to #include <inttypes.h> and use: "No type promotion here: %f %"PRIu16" %"PRIx8"\n"