Question

I know that the operators . and -> have the same precedence, so how the following expression evaluated?

c . e -> d . f == a . b

Was it helpful?

Solution

When dealing with operators of the same precedence refer to the operator associativity - http://en.wikipedia.org/wiki/Operator_associativity

For example plus and minus associate to the left, 5 + 6 - 7 = (5 + 6) - 7.
Some operators on the otherhand associate to the right, an example of this is the assignment operator.
a = b = c is equivalent to a = (b = c). In this case b = c returns c and allows you to chain assignments. As with operator precedence rules these can be hard to remember so when unsure put everything in parentheses.

In your case all the operators associate to the left so

c.e->d.f == a.b is equivalent to (((c.e)->d).f) == (a.b)

OTHER TIPS

Well, just like you said, .. and -> have the same precedence, which is higher than that of ==. That means that the expression you posted stands for equality comparison between c.e->d.f and a.b.

c.e->d.f stands for operator -> appied to the value of c.e. And then in turn . is applies to the value of c.e->d.

In other words, the whole thing is equivalent to

(((c.e)->d).f) == (a.b)

Note that the () only indicate grouping between operators and their operands. There are no guarantees of any kind about the run-time order of evaluation.

The syntax for postfix expressions using component-selection operators is as follows:

6.5.1 Primary expressions

Syntax
1 primary-expression:
    identifier
     ...
6.5.2 Postfix operators

Syntax

1 postfix-expression:
    primary-expression
    ...
    postfix-expression . identifier
    postfix-expression -> identifier
    ...

Based on the syntax, component selection operators are left-associative. So an expression like a->b.c would be parsed as


                    a    ->     b      .      c
                    |           |             |
                identifier      |             |
                    |           |             |
                  primary       |             |
                expression      |             |  
                    |           |             |
                 postfix        |             |
                expression  identifier        |
                    |           |             |
                    +-----+-----+             |
                          |                   |
                       postfix           identifier
                     expression               |
                          |                   |
                          +---------+---------+
                                    |
                            postfix expression

or (a->b).c. The leftmost . or -> binds first, and any remaining . or -> bind to the result of the first. Thus, your example would parse as

(((c.e)->d).f) == (a.b)

since . and -> have higher precedence than ==.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top