Pointer operations and operator precedence in C

后端 未结 6 811
野性不改
野性不改 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:10

    You and the other guy both are wrong!

    Either,
    1. both pointers increment first and then assignment takes place or
    2. one pointer increments, then the assignment happens, then the other pointer increments or
    3. first assignment takes place and then pointers increments.

    But the rule is that, all the side effects of one statement have to be complete before the next statement starts. Keep in mind that the original values must be used. As long as the original value is used, the increment can happen at any time.

    C-faq: 3.2:

    It is not guaranteed that an increment or decrement is performed immediately after giving up the previous value and before any other part of the expression is evaluated. It is merely guaranteed that the update will be performed sometime before the expression is considered "finished".

    Read this answer given by Eric Lippert for detailed explanation.

提交回复
热议问题