[Solved] How to understand [], (), compound literal operator, . and -> are postfix operators?


  1. The language specification defines the syntax, and doesn’t care what you call the parts that make up the language.

    In abc->def, the first operand is abc, and -> is the operator and def is the second operand, but it is restricted to an identifier is the name of a member of the structure or union of the type that the first operand (which may be a full-blown postfix-expression) identifies. You can’t write general expressions on the RHS of the -> operator — abc->3 doesn’t work, and neither does abc->(x + y). Similar comments apply to the . operator, mutatis mutandis.

    If you want to think of this as an infix operator, you may do so. That’s your privilege. It won’t help you understand the standard.

  2. (a) The [] operator follows a postfix expression; it is after the postfix expression; that makes it a postfix operator.
    (b) The () function-call operator follows a postfix expression; it is after a postfix expression; that makes it a postfix operator.
    (c) The compound literal operator is all of (type){ and }. Since the { … } comes after the (type) part, and since it behaves like other postfix operators in the syntax, it is reasonable to regard it as a postfix operator. The standard says it is one; it really doesn’t matter anyway. The language works as specified.

  3. The cast operators are separately grouped, and a kind of ‘prefix operator’. They are distinct from compound literals. The expression after a cast cannot start with an {; the literal after the ‘cast notation’ in a compound literal can only start with a {. The presence or absence of { is determinative of whether the construct is a cast or a compound literal.

All of this is pretty obvious if you read and understand the standard.


  1. (a) and (b) The C standard doesn’t say that “it is after the postfix expression; that makes it a postfix operator.” I am not sure what that mean either. In general outside C, an operator is called postfix, if it appears after all its operands.

The standard says that one of the ways of building a postfix-expression is:

postfix-expression [ expression ]

From where I’m sitting, “The [] operator follows a postfix expression; it is after the postfix expression; that makes it a postfix operator” seems a reasonably accurate description of that portion of the syntax. The [ and ] appear after a prior postfix-expression. I’m not sure what else you’d understand by a ‘postfix operator’ than ‘an operator that comes after something’, and the [] comes after ‘something’.

2(c) my intuition from reading the books on C so far suggests that initialization parts are not operands or operators, so I doubt that {} is part of the operator of compound literal.

A compound literal consists of a type name enclosed in round brackets (parentheses) followed by an initializer list enclosed in curly brackets (braces). It takes all of that to make a compound literal. I don’t see how the {} could not be a part of the compound literal — the syntax says it is, and … well, if you want to think of it as something separate, I can’t stop you, but you’re making your own life more difficult by doing so and making life very difficult for those people on SO trying to help you. It really isn’t clear from this side of my screen how you manage to come up with these new ways to misinterpret the plain text of the standard so cleverly and deviously.

4

solved How to understand [], (), compound literal operator, . and -> are postfix operators?