Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Compound literals are fun. You can use them to implement labelled and optional parameters to functions too:

    struct args { int a; char *b; };
    #define fn(...) (fn_((struct args){__VA_ARGS__}))

    void fn_ (struct args args) {
        printf ("a = %d, b = %s\n", args.a, args.b);
    }

    fn (0, "test");
    fn (1); // called with b == NULL
    fn (.b = "hello", .a = 2);
(As written this has a subtle catch that fn() passes undefined values, but you can get around that by adding an extra hidden struct field which is always zero).


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: