Multidimensional arrays memory storage overview

前端 未结 4 1544
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-28 05:01

Lately I started learning pointers and references in c++ (not just the usual use of them,but all kinds of ways,I don\'t want to have problems with them in near future).

4条回答
  •  心在旅途
    2021-01-28 05:40

    t is a pointer declared on the stack which holds the address of an array on the heap. &t gets the address of t, AKA the location of t on the stack. If you want to access the first member of the array, you can either do *t or t[0]. C++ automatically dereferences and does pointer arithmetic based on the type of t when you use the bracket notation.

    However, &t[0] essentially gets you the address of t[0], which is why it is different from &t, as &t lives on the stack while &t[0] lives on the heap. So if you want the address of t[0], you can use that notation, otherwise stick to t[0] if you just want the values.

提交回复
热议问题