Compound Literals
A compound literal is a type name enclosed in parentheses followed by an initialization list. It creates an unnamed value of the specified type, which has scope limited to the block in which it is created or global scope if defined outside any block. In the latter case, the initializers must all be constant expressions.
Consider an example:
(struct date) { .month = 7, .day = 2, .year = 2011}
This expression produces a structure of type struct date with the specified initial values. You can assign this to another struct date structure, like so:
theDate = (struct date) { .month = 7, .day = 2, .year = 2011} ;
Or you can pass it to a function or method that expects an argument of struct date, like so:
setStartDate ((struct date) ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access