Operator 'sizeof' with conditional (ternary) expression

后端 未结 3 433
野的像风
野的像风 2021-01-17 10:53

I have a hard time understanding sizeof\'s behaviour when given a ternary expression.

#define STRING \"a s         


        
3条回答
  •  遥遥无期
    2021-01-17 11:39

    You have to understand expressions, which are the core component of the language.

    Every expression has a type. For an expression e, sizeof e is the size of the type of the value of the expression e.

    The expression a ? b : c has a type. The type is the common type of the two operand expressions b and c.

    In your example, the common type of char[9] and char[1] is char * (both array-valued expressions decay to a pointer to the first element). (In C++, the rules for string literals are different and there is a const everywhere.)

提交回复
热议问题