Using sizeof operator on a typedef-ed struct

后端 未结 3 1472
攒了一身酷
攒了一身酷 2021-02-20 13:05

This might be something too obvious. However, I couldn\'t find the specific answer though many stackoverflow threads talk about different aspects of this.

typede         


        
相关标签:
3条回答
  • 2021-02-20 13:19

    add parentheses around tmp: sizeof(tmp)

    0 讨论(0)
  • 2021-02-20 13:43

    The sizeof operator have two forms:

    sizeof expression
    sizeof(type)
    

    As you're giving it a type, you need the parenthesis, sizeof(tmp)

    0 讨论(0)
  • 2021-02-20 13:44

    5.3.3 Sizeof [expr.sizeof]

    1) The sizeof operator yields the number of bytes in the object representation of its operand. The operand is either an expression, which is an unevaluated operand (Clause 5), or a parenthesized type-id. (emphasis mine)

    In your case, it is a type-id so it must be parenthesized. What a type-id is is described in 8.1 Type names [dcl.name].

    sizeof tmp should be sizeof (tmp).

    As in

    if (c <= sizeof tmp) should be if (c <= sizeof (tmp)).

    Yup, pretty "obvious and straightforward".

    0 讨论(0)
提交回复
热议问题