c / expert
Snippet
C99 Compound Literals for Temporary Objects
Compound literals allow creating unnamed objects on the fly. They are lvalues, meaning you can even take their address. This is extremely useful for passing complex arguments to functions without cluttering the scope with temporary variables.
snippet.c
1
2
3
4
5
6
7
8
typedef struct { int r, g, b; } Color;void apply_color(Color c);int main() {// Passing an anonymous struct object without a variableapply_color((Color){255, 128, 0});return 0;}
Breakdown
1
(Color){255, 128, 0}
Creates a temporary Color struct instance with the specified values.