C pointers and arrays/ 'sizeof' operator [duplicate]

寵の児 提交于 2019-11-27 09:15:22

Yes, your assumption is correct, assuming an int and a pointer are both 4 bytes long on your machine.

And no, arrays aren't pointers. The array name sometimes decays into a pointer in certain contexts, but they aren't the same thing. There is a whole section of the comp.lang.c FAQ dedicated to this common point of confusion.

The size of the array is not stored in memory in either case, whether you declare it as int myArr[20] or int* myArrPtr.

What happens is that sizeof() is replaced (by the compiler) with a constant value.

Therefore, because myArr was specified with a fixed size prior to compilation, the compiler knows just how big the amount of memory allocated is. With myArrPtr, you could dynamically allocate different array sizes, so only the size of the type is stored.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!