What is the syntax of the C function?

I would say that I have intermediate programming experience in c, however I have never seen this syntax used before to make a function. This is reminiscent of the syntax of a jQuery event. All in all, I would like to get a detailed explanation of what it is and what might be an alternative syntax. A link to where I could learn more about this, in particular, would be wonderful 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 a Pebble WatchApp tutorial .

+6
source share
2 answers

This is a function call using a composite literal. This 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 use designated initializers, where you can specify the fields to initialize by name.

Assuming WindowHandlers only contains 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 ); 

C standard is discussed in more detail in them.

From section 6.5.2.5:

4 A postfix expression consisting of a type in parentheses is a name followed by a list of initializers enclosed in parentheses is a composite literal. It provides an unnamed object whose value is equal to that specified in the list of initializers.

...

9 EXAMPLE 1 Defining a file area

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

initializes p to point to the first element of the array two ints, the first of which has a value of two, and the second of four. The expressions in this composite literal must be constant. An unnamed object has a static storage duration.

From section 6.7.8:

one

 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 the designation has the form

 .identifier 

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

...

34 EXAMPLE 10 Elements of a structure can be initialized with nonzero values, regardless of their order:

 div_t answer = { .quot = 2, .rem = -1 }; 
+10
source

This is a standard from C99 and above. It combines complex literals:

 (WindowHandlers) {} 

and designated initializers:

 .load = main_window_load, .unload = main_window_unload 

See the link. What does the syntax of this point mean in the Pebble watch development guide?

+5
source

All Articles