How does sizeof know the size of the operand array?

前端 未结 12 1222
臣服心动
臣服心动 2020-12-03 13:16

This may be a stupid question but how does the sizeof operator know the size of an array operand when you don\'t pass in the amount of elements in the array. I know it does

12条回答
  •  北海茫月
    2020-12-03 13:51

    The problem underlying your trouble to understand this might be because you are confusing arrays and pointers, as so many do. However, arrays are not pointers. A double da[10] is an array of ten double, not a double*, and that's certainly known to the compiler when you ask it evaluate sizeof(da). You wouldn't be surprised that the compiler knows sizeof(double)?

    The problem with arrays is that they decay to pointers to their first elements automatically in many contexts (like when they are passed to functions). But still, array are arrays and pointers are pointers.

提交回复
热议问题