Pointer operations and operator precedence in C

后端 未结 6 810
野性不改
野性不改 2021-02-04 03:32

Background

Just had a chat with a C guy today and we disagreed on the following:

int intgA[2] = { 1, 2 };
int intgB[2] = { 3, 5 };

int *intAPtr = intg         


        
6条回答
  •  攒了一身酷
    2021-02-04 04:29

    The main effect of the ++ operator is to produce the value of its operand (without having incremented it). The side effect is to increment the operand.

    Even if the side effect is performed before the value produced by the main effect is used in another operation, the value produced by the main effect is the original value of the operand, not the updated value.

    In *x++, which is parsed as *(x++), the * operator is applied to the value produced by the main effect, so the * operation is the same as if *x is evaluated. The increment happens “on the side”, outside of the main expression evaluation, so it takes no part in determining the value of *x++.

提交回复
热议问题