### Question 3.1

Why doesn't this code:

    a[i] = i++;

work?

The subexpression `i++` causes a side effect--it modifies `i`'s value--which leads to undefined behavior since `i` is also referenced elsewhere in the same expression. (Note that although the language in K&R suggests that the behavior of this expression is unspecified, the C Standard makes the stronger statement that it is undefined--see question 11.33.)

References: K&R1 Sec. 2.12
K&R2 Sec. 2.12
ANSI Sec. 3.3
ISO Sec. 6.3

Read sequentially: prev next up top

This page by Steve Summit // Copyright 1995 // mail feedback