Page 120 - C-Language
P. 120

int *p = (int [2]){ 2, 4 };


        p is initialized to the address of the first element of an unnamed array of two ints.

        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.


               void f(void)
               {
                   int *p;
                   /*...*/
                   p = (int [2]){ *p };
                   /*...*/
               }


              p is assigned the address of the first element of an array of two ints, the first having the
              value previously pointed to by p and the second, zero.[...]


        Here p remains valid until the end of the block.


        Compound literal with designators




        (also from C11)


           struct point {
             unsigned x;
             unsigned y;
           };

           extern void drawline(struct point, struct point);

          // used somewhere like this
          drawline((struct point){.x=1, .y=1}, (struct point){.x=3, .y=4});


        A fictive function drawline receives two arguments of type struct point. The first has coordinate
        values x == 1 and y == 1, whereas the second has x == 3 and y == 4



        Compound literal without specifying array


        length




         int *p = (int []){ 1, 2, 3};


        In this case the size of the array is no specified then it will be determined by the length of the
        initializer.






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