How Are C Arrays Represented In Memory?

后端 未结 8 729
醉话见心
醉话见心 2020-12-14 01:33

I believe I understand how normal variables and pointers are represented in memory if you are using C.

For example, it\'s easy to understand that a pointer Ptr will

8条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-14 01:55

    Yes, you've got it. A C array finds the indexed value x[y] by calculating x + (y * sizeof(type)). x is the starting address of the array. y * sizeof(type) is an offset from that. x[0] produces the same address as x.

    Multidimensional arrays are similarly done, so int x[y][z] is going to consume sizeof(int) * y * z memory.

    Because of this you can do some stupid C pointer tricks. It also means getting the size of an array is (almost) impossible.

提交回复
热议问题