3

I came across following code in Pebble watch app development tutorial:

// 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
  });

I cant understand this assignment to .load and .unload. Is this standard C? I don't think I have ever seen similar syntax before.

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
binW
  • 13,220
  • 11
  • 56
  • 69

2 Answers2

7

This is standard c99.

It is combining compound literals

 (WindowHandlers) {}

and designated initializers

.load = main_window_load,
.unload = main_window_unload
2501
  • 25,460
  • 4
  • 47
  • 87
4

I believe it is standard C99, with an initialized struct constant with named fields in its initialization.

BTW, it is also a C extension -w.r.t. older C standards- (designated initializers) provided by GCC

For C11 standard, its final draft n1570 describes that syntax in "§6.7.9 Initializations"

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • 3
    "Also a C extension" is confusing and makes it sound like this is not standard, contrary to reality. Did you mean to say GCC also offers it as an extension for older versions of C? Or something else? – R.. GitHub STOP HELPING ICE Nov 15 '14 at 18:50