c++ trying to understand clockwise rules for deciphering complicated syntax

后端 未结 2 741
孤独总比滥情好
孤独总比滥情好 2021-01-07 14:13

I have the following code:

int ia[3][4] = {    //
{0, 1, 2, 3},   //
{4, 5, 6, 7},   //
{8, 9, 10, 11}  //
};

int (*p4)[4] = ia;
cout << \"(*(p4 + 0))         


        
2条回答
  •  情深已故
    2021-01-07 14:48

    Postfix expressions have higher priority than unary expressions.

    So this expression

    *(p4 + 0)[3]
    

    is equivalent to the expression

    *( (p4 + 0)[3] )
    

    As it is seen it is not the same as

    ( *(p4 + 0) )[3]
    

    This expression *( (p4 + 0)[3] ) can be written just like *p4[3] that is equivalent to *ia[3] and due to the priority is considered like

    *( ia[3] )
    

    or like

    ia[3][0]
    

    The valid range for indices of the first dimension for the array ia is [0, 2] because the array is declared like

    int ia[3][4] = { /*...*/ };
          ^^^
    

    Thus expression ia[3] tries to access memory beyond the array. As result the code snippet has undefined behavior.

    Shortly speaking you should understand that these expressions

    (*(p4 + 0))[3]
     *(p4 + 0)[3]
    

    that in turn are equivalent to the following pair of expressions

    ia[0][3]
    io[3][0]
    

    are not equivalent due to the priorities of the operations.

提交回复
热议问题