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 = 2014}

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 = 2014} ;

Or you can pass it to a function or method that expects an argument ...

Get Programming in Objective-C, Sixth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.