6

I'd say I have intermediate experience with programming in c, however I've never seen this syntax used before to make a function. This reminds me of the syntax for a JQuery event. Overall, I'd like a detailed explanation of what this is and what the alternative syntax could be.A link to where I could read more about this in particular would be great too.

 // Set handlers to manage the elements inside the Window
 window_set_window_handlers(s_main_window, (WindowHandlers) {
    .load = main_window_load,
    .unload = main_window_unload
  });

This is a code snippet from the Pebble WatchApp tutorial.

lavacode
  • 155
  • 2
  • 9
  • I think it's making a struct, casting to `WindowHandlers`, and passing it into that function – Alex Aug 11 '16 at 21:51
  • 3
    @Alex - No it's instead a compound literal. And btw this is a function call AFAIK. No function is made but instead one is called. The second function argument is a compound literal. – AnArrayOfFunctions Aug 11 '16 at 21:53
  • 2
    `window_set_window_handlers` have 2 parameters `s_main_window` and a struct `WindowHandlers`, the struct `WindowHandlers` is initialized here – fedi Aug 11 '16 at 21:55
  • @CisNOTthatGOODbutISOisTHATBAD I worded it badly. That's basically what I meant. I was drawing parallels from C++'s initialization lists but didn't know what it was called – Alex Aug 11 '16 at 21:57
  • @Alex: There is no cast in a compound literal. A cast is a completely different beast. – too honest for this site Aug 11 '16 at 22:22
  • @Olaf Thanks, I'll keep that in mind. Again, I knew nothing about compound literals. It looked like a mix of init list and a cast. I figured that it yielded a `WindowHandler`. That's the only thing I was sure of. When I wrote that there was a cast, it was more of a fuzzy thing that I absentmindedly tossed in as I made further guesses. Not meant to be taken seriously – Alex Aug 11 '16 at 22:59
  • @BruceDavidWilner: Stop YELLING! And learn C, it **is** C - and very strictly! – too honest for this site Aug 11 '16 at 23:15

2 Answers2

10

This is a function call making use of a compound literal. It is equivalent to the following:

WindowHandlers temp = {
    .load = main_window_load,
    .unload = main_window_unload
  };
window_set_window_handlers(s_main_window, temp );

The above also makes use of designated initializers, where you can specify fields to initialize by name.

Assuming WindowHandlers contains only load and unload in that order, the above is equivalent to:

WindowHandlers temp = { main_window_load, main_window_unload };
window_set_window_handlers(s_main_window, temp );

The C standard goes into these in more detail.

From section 6.5.2.5:

4 A postfix expression that consists of a parenthesized type name followed by a brace-enclosed list of initializers is a compound literal. It provides an unnamed object whose value is given by the initializer list.

...

9 EXAMPLE 1 The file scope definition

int *p = (int []){2, 4};

initializes p to point to the first element of an array of two ints, the first having the value two and the second, four. The expressions in this compound literal are required to be constant. The unnamed object has static storage duration.

From section 6.7.8:

1

initializer:
    assignment-expression
    { initializer-list }
    { initializer-list , }
initializer-list:
    designationopt initializer
    initializer-list , designationopt initializer
designation:
    designator-list =
designator-list:
    designator 
    designator-list  designator
designator:
    [ constant-expression ]
    .identifier

...

7 If a designator has the form

.identifier

then the current object (defined below) shall have structure or union type and the identifier shall be the name of a member of that type.

...

34 EXAMPLE 10 Structure members can be initialized to nonzero values without depending on their order:

div_t answer = { .quot = 2, .rem = -1 };
dbush
  • 205,898
  • 23
  • 218
  • 273
5

This is standard from C99 onwards. It is combining compound literals:

(WindowHandlers) {}

and designated initializers:

.load = main_window_load,
.unload = main_window_unload

See the link What does this dot syntax mean in the Pebble watch development tutorial?

Community
  • 1
  • 1