Page 119 - C-Language
P. 119

Chapter 14: Compound Literals




        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.


        Examples



        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:





        https://riptutorial.com/                                                                               95
   114   115   116   117   118   119   120   121   122   123   124