Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is this C function syntax? [duplicate]

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.

like image 558
lavacode Avatar asked Aug 11 '16 21:08

lavacode


People also ask

What is duplicate function declaration?

Duplicate declarations are simply an equivalent restatement of an interface specification, and that has no effect on code which depends on that interface.

Is there a copy function in C?

(Copy Memory Block) In the C Programming Language, the memcpy function copies n characters from the object pointed to by s2 into the object pointed to by s1. It returns a pointer to the destination.

What is C function?

Functions in C are the basic building blocks of a C program. A function is a set of statements enclosed within curly brackets ({}) that take inputs, do the computation, and provide the resultant output. You can call a function multiple times, thereby allowing reusability and modularity in C programming.

What is main () in C?

Every C program has a primary function that must be named main . The main function serves as the starting point for program execution. It usually controls program execution by directing the calls to other functions in the program.


1 Answers

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 };
like image 180
dbush Avatar answered Sep 27 '22 23:09

dbush