C LanguageCompound Literals

Download C Language for free

Syntax

  • (type){ initializer-list }

Remarks

C standard says in C11-§6.5.2.5/3:

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.99)

and footnote 99 says:

Note that this differs from a cast expression. For example, a cast specifies a conversion to scalar types or void only, and the result of a cast expression is not an lvalue.

Note that:

String literals, and compound literals with const-qualified types, need not designate distinct objects.101)

101) This allows implementations to share storage for string literals and constant compound literals with the same or overlapping representations.

Example is given in standard:
C11-§6.5.2.5/13:

Like string literals, const-qualified compound literals can be placed into read-only memory and can even be shared. For example,

(const char []){"abc"} == "abc"

might yield 1 if the literals’ storage is shared.

Related Examples

Definition/Initialisation of Compound Literals