Why is arr and &arr the same?

前端 未结 6 730
鱼传尺愫
鱼传尺愫 2020-12-07 21:12

I have been programming c/c++ for many years, but todays accidental discovery made me somewhat curious... Why does both outputs produce the same result in the code below? (<

6条回答
  •  -上瘾入骨i
    2020-12-07 21:49

    Pointers and arrays can often be treated identically, but there are differences. A pointer does have a memory location, so you can take the address of a pointer. But an array has nothing pointing to it, at runtime. So taking the address of an array is, to the compiler, syntactically defined to be the same as the address of the first element. Which makes sense, reading that sentence aloud.

提交回复
热议问题