
Named parameters in C - guillaumec
http://blog.noctua-software.com/named-parameters-in-c.html
======
nitrogen
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});

~~~
codehero
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...

~~~
nitrogen
_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.

------
abcd_f
Clever, but seems like a solution in a search of a problem.

~~~
davekeck
The problem is that arguments are often ambiguous without the accompanying
function declaration. (And the problem's exasperated when there are lots of
arguments.)

The solution is named arguments, but I'm not convinced the added complexity of
this solution is a net improvement.

~~~
userbinator
When writing in C, usually you either know the function well enough (including
having just written it) to have memorised the arguments, have another window
containing the declarations, or else you'll be using an IDE that tells you
what the arguments are (along with a lot of other information.)

~~~
nitrogen
The most frustrating thing in C or C++ is when a header file is missing all of
the argument names, so the declarations are just

    
    
        OVERKILL_TYPEDEF_PTR_T somefunc(TYPE1_T, TYPE2_PTR_T, void *);
    

I always document my functions in the headers, especially for private code
only I will use. E.g.

    
    
        /*
         * Like vfprintf(), but prepends a timestamp and the name of the current thread
         * or process.  The output FILE will be locked using lockf() while the
         * timestamp, thread name, and message are written.  The thread name can be set
         * using set_threadname().
         */
        int vfptmf(FILE *out, const char *format, va_list args);

------
hyperliner
I think it is an interesting article as a lesson on how macro expansion works.

It is however, a terrible example of how to write good code. Any code that
hides the language or manipulates for syntactic sugar only adds complexity,
bugs and decreases the maintenability of the code.

As someone already said, a solution in search of a problem.

------
eq-
It's not (strictly) C, though.

------
xuhu
Doesn't work in MSVC though.

~~~
pling
That's not a proper C compiler though, as Microsoft like to point out
regularly as a lame excuse...

~~~
fdej
Yet enough people think it's a C compiler that if you try to use avant garde
C99 features in your C project, someone will invariably complain that MSVC
doesn't build it.

~~~
ibisum
.. thus giving up C and moving on to C# instead. (Students, I'm looking at
you..)

