Which issues have you encountered due to sequence points in C and C++?

后端 未结 6 490
栀梦
栀梦 2021-02-04 20:39

Below are two common issues resulting in undefined behavior due to the sequence point rules:

a[i] = i++; //has a read and write between sequence points
i = i++;          


        
6条回答
  •  太阳男子
    2021-02-04 21:34

    Here is a simple rule from Programming principles and practices using c++ by Bjarne Stroustup

    "if you change the value of a variable in an expression.Don't read or write twice in the same expression"

    a[i] = i++; //i's value is changed once but read twice
    i = i++;   //i's value is changed once but written twice
    

提交回复
热议问题