C Language Compound Literals

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

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.



Got any C Language Question?