Getting the size of member variable

前端 未结 7 400
猫巷女王i
猫巷女王i 2020-12-15 17:34

If there is a POD structure, with some member variables, for example like this:

struct foo
{
   short a;
   int b;
   char c[50];
   // ...
};
7条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-15 18:18

    5.3.3/1:

    The sizeof operator yields the number of bytes in the object representation of its operand. The operand is either an expression, which is not evaluated, or a parenthesized type-id.

    The above means the following construct is well defined:

    sizeof( ((foo *) 0)->a);
    

提交回复
热议问题