I have the following code
int m[4]={1,2,3,4}, *y;
y=m;
*y = f(y++); // Expression A
My friend told me that Expression A has a well defined behavior but I am not sure whether he is correct.
According to him function f() introduces a sequence point in between and hence the behavior is well defined.
Someone please clarify.
P.S: I know we should not write such code for practical purpose. It is just for the purpose of learning. 🙂
At best, the code in question has unspecified behavior. For the assignment operators, “the order of evaluation of the operands is unspecified” (C99 §6.5.16/4).
If the left operand is evaluated first, the result of
f(y++)will be stored inm[0]. If the right operand is evaluated first, the result will be stored inm[1].As for whether the behavior is undefined, the relevant paragraph is:
If the left side is evaluated first, then we run afoul of the second sentence because the ordering is:
yis read on the left side to dereference ityis read on the right side to increment ity++is complete andyis written to)In step 1, the “prior value” of
yis read but for a purpose other than “determining the value to be stored.” Thus, the behavior is indeed undefined because one valid evaluation order yields undefined behavior.