a
is an array, foo
is a function, and i
is an int
.
a[++i] = foo(a[i-1], a[i]);
Would the code above, have an Undefined Behavior?
The array indices ++i
, i-1
and i
, are guaranteed to be in the array range.
arraysc++incrementlanguage-lawyerundefined-behavior
a
is an array, foo
is a function, and i
is an int
.
a[++i] = foo(a[i-1], a[i]);
Would the code above, have an Undefined Behavior?
The array indices ++i
, i-1
and i
, are guaranteed to be in the array range.
Best Answer
The behavior is undefined, but it's not because of the modification of the same object twice between two sequence points but it is UB because the side effect on
i
is unsequnced relative to the value computation ofa[i-1]
anda[i]
usingi
.ยง6.5-p(2):
For example, expression
invokes undefined behavior. Same is true for