Does this C++ code produce an undefined behavior?

This question already has an answer here:

  • With arrays, why is it the case that a[5] == 5[a]? 17 answers
  • This doesn't look like a function. What is this? 5 answers

  • It's perfectly legal. With pointer arithmetic, a[42] is equivalent to *(a + 42) , which (addition being commutative) is equivalent to *(42+ a) , which (by definition of [] ) is equivalent to 42[a] .

    So it's obscure, but well-defined.


    The array operator is commutative.

    a[n] == *(a + n) == *(n + a) == n[a]

    And it's perfectly legal.


    a[i] is defined as *(a+i) .

    So 42[a] = a[42]; and it is perfectly safe

    链接地址: http://www.djcxy.com/p/86728.html

    上一篇: 我可以在哪里放置数组下标?

    下一篇: 这个C ++代码是否产生未定义的行为?