Search code examples
coperator-precedence

Precedence of the arrow operator and post increment?


New to C. So I saw this line of code

system->word[system->index++] = system->next_char;

Is it equivalent to:

system->word[system->index] = system->next_char;
index++;

What is the precedence for post increment? Does it only increment index's value by 1 after all the operations on the line are done executing?


Solution

  • Updating system->index is defined as a side effect that is not sequenced (is not specified to come before or after) the other operations in the statement. The update may occur before, during, or after other operations.

    The fact that it is not sequenced is irrelevant as long as it is not used elsewhere in the statement, because, if it is not used elsewhere, then nothing the statement does can be affected by when the update occurs. (Note that, even if the update to system->index in memory is done before the value is used, the compiler is response for ensuring that the pre-update value is used.)

    If the object being updated were used elsewhere in the statement in an unsequenced way (that is, no rule specifies which comes first, the update or the other use), then the behavior of the program would not be defined by the C standard.

    This is not a consequence of precedence. Precedence determines the structure of how expressions are interpreted, not the sequencing of their operations.