Does this C++ code produce an undefined behavior?
This question already has an answer here:
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
上一篇: 我可以在哪里放置数组下标?
下一篇: 这个C ++代码是否产生未定义的行为?