Is “*p = ++(*q)” undefined when p and q point to the same object?

后端 未结 5 1644
天命终不由人
天命终不由人 2021-02-13 22:02

after reading about sequence points, I learned that i = ++i is undefined.

So how about this code:

int i;
int *p = &i;
int *q = &i;
          


        
5条回答
  •  庸人自扰
    2021-02-13 22:39

    The best tool not to detect, but to avoid this in the first place is to use good programming practice. Avoid side-effects and do no more than one write per assignment. There is nothing wrong with

    *q += 1;
    *p = *q;
    

提交回复
热议问题