News for hackers. Stuff that matters.
Re:Named parameters in C by nitrogen on Monday July 14, 2014 @06:12AM (#8030258)
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});
Re:Named parameters in C by codehero on Monday July 14, 2014 @12:08PM (#8031567)
That 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...

Re:Named parameters in C by nitrogen on Monday July 14, 2014 @01:01PM (#8031883)
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...

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.

Re:Named parameters in C by Peaker on Monday July 14, 2014 @02:26PM (#8032374)
Note, %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"
Re:Named parameters in C by CountHackulus on Monday July 14, 2014 @10:00AM (#8030924)
And that's almost exactly what the macro expands to. It's just some nice syntactic sugar on top.
Re:Named parameters in C by nitrogen on Monday July 14, 2014 @01:02PM (#8031893)
The 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.
Re:Named parameters in C by eliteraspberrie on Monday July 14, 2014 @09:48AM (#8030851)
This (compound literals) also has the advantage of being strictly valid C, since compound statements are a GNU extension.
Re:Named parameters in C by micampe on Monday July 14, 2014 @10:02AM (#8030928)
I 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.
Re:Named parameters in C by comex on Monday July 14, 2014 @10:17AM (#8031002)
Compound literals are valid C; compound statements, used in the macros in the article, are a GNU extension.
Re:Named parameters in C by micampe on Monday July 14, 2014 @10:44AM (#8031138)
Oh right, I misread the previous comment, thanks.
Re:Named parameters in C by dsego on Monday July 14, 2014 @07:12AM (#8030373)
What's the point in that? Then you have to write all your functions to accept structs instead of natural arguments.
Re:Named parameters in C by ygra on Monday July 14, 2014 @08:20AM (#8030524)
http://c2.com/cgi/wiki?ParameterObject

Sometimes it's a good way to go.

Re:Named parameters in C by simgidacav on Monday July 14, 2014 @12:03PM (#8031535)
...agreed, what's the point? Besides, a lot of includes are already a mess of #defines, do we really need another bunch of them?
Re:Named parameters in C by peaton on Monday July 14, 2014 @09:49AM (#8030865)
Wow, 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.
Re:Named parameters in C by micampe on Monday July 14, 2014 @09:59AM (#8030921)
A value of 0 is not the same thing as a missing value.
Re:Named parameters in C by mpyne on Monday July 14, 2014 @10:47AM (#8031152)
That's true, but the parent comment never claimed that, only that missing arguments are assigned the value 0 in the resultant struct.
Re:Named parameters in C by micampe on Monday July 14, 2014 @11:00AM (#8031205)
“[…] so you get optional parameters for free”
Re:Named parameters in C by nitrogen on Monday July 14, 2014 @11:36AM (#8031400)
I should have finished that with "... as long as a default of 0 is acceptable." If 0 is a valid argument but a bad default, then it's slightly less useful.