Does 'a[i] = i;' always result in well defined behaviour?

后端 未结 6 1403
忘了有多久
忘了有多久 2021-02-05 11:19

There are several interesting questions raised here regarding undefined behaviour in C. One of them is (slightly modified)

Does the following piece of code r

6条回答
  •  一向
    一向 (楼主)
    2021-02-05 12:05

    No it doesn't. The first line has a sequence point (the comma), so it is not undefined behaviour:

    int i = 0, *a = &i;
    

    The second line is perfectly normal.

    a[i] = i + 1;
    

    Since i + 1 creates a temporary value, i gets modified only once, on the assignment. This however would be undefined behaviour:

    a[i] = i++;
    

提交回复
热议问题