a
is an array, foo
is a function, and i
is an int
.
a[++i] = foo(a[i-1], a[i]);
Would
Yes, it is Undefined Behavior.
Not because one is not allowed to read and write to the same variables between 2 sequence points but because of the rule that
Between any two sequence points, all the reads of a variable should directly be used in the computing the result of the write to the same variable.
Here the write to i
is i++
. The read on the same variable is in the arguments.
Although function call is a sequence point, the assignment isn't. So evaluation of array index can happen before evaluation of RHS.
The read on i
in foo(a[i-1], a[i])
doesn't directly contribute to the write and hence it is UB.
the relevant parts of the C99 standard are 6.5 Expressions, §2
Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be read only to determine the value to be stored.
(Emphasis mine)