Sololearn: Learn to Code
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3
In the first case, a is an array with a known bound, so the sizeof operator will return its total size in bytes, which is 40 bytes. On the other hand, in the second one, a is an array with an unknown bound, so it's just basically a pointer, the sizeof operator will return the size of a pointer, which is 8 bytes, not 2, in 64 bit system
23rd Aug 2019, 10:31 AM
Agent_I
Agent_I - avatar
+ 2
The size of a pointer itself is independant of what it points to. You can refer to the following question: https://www.sololearn.com/discuss/1920886/?ref=app Or the following article: https://www.quora.com/What-is-the-size-of-a-pointer-in-C The reason for the varying size is that sizeof() behaves differently based on the argument. If you look at the reference https://en.cppreference.com/w/cpp/language/sizeof you can see that "it yields the size in bytes of the object representation". For a pointer, that is the size of the pointer, which is pretty much fixed as stated above. For an array, however, the region of storage covers all elements inside the array, hence the size returned equals numberOfElements * sizeOfElementType. That is why there is such a big difference is size.
23rd Aug 2019, 10:38 AM
Shadow
Shadow - avatar