| Start |
|---|
| Definition/Initialisation of Compound Literals A compound literal is an unnamed object which is created in the scope where is defined. The concept was first introduced in C99 standard. An example for compound literal is Examples from C standard, C11-§6.5.2.5/9:
The compound literal is an lvalue. The storage duration of the unnamed object is either static (if the literal appears at file scope) or automatic (if the literal appears at block scope), and in the latter case the object's lifetime ends when control leaves the enclosing block.
Here Compound literal with designators(also from C11) A fictive function Compound literal without specifying array lengthIn this case the size of the array is no specified then it will be determined by the length of the initializer. Compound literal having length of initializer less than array size specifiedrest of the elements of compound literal will be initialized to Read-only compound literalNote that a compound literal is an lvalue and therefore it's elements can be modifiable. A read-only compound literal can be specified using Compound literal containing arbitrary expressionsInside a function, a compound literal, as for any initialization since C99, can have arbitrary expressions. Syntax
RemarksC 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: Like string literals, const-qualified compound literals can be placed into read-only memory and can even be shared. For example, might yield 1 if the literals’ storage is shared. |
Read Article
https://develop.horje.com/learn/1434/reference