How does sizeof know the size of the operand array?

前端 未结 12 1226
臣服心动
臣服心动 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 14:03

    Sizeof can only be applied to completely defined types. The compiler will either be able to determine the size at compile time (e.g., if you had a declaration like int foo[8];), or it will be able to determine that it has to add code to track the size of a variable-length array (e.g., if you had a declaration like int foo[n+3];).

    Contrary to other answers here, note that as of C99, sizeof() is not necessarily determined at compile time, since arrays may be variable-length.

提交回复
热议问题