Search code examples
coperatorsoperator-precedenceassociativity

How to interpret the operator associativity?


Parentheses and pointer symbol have same priority, and they are dealed from left to right. Why does the following code try to get the member nfct from skb, then do the type conversion? It's seems that the associativity is from right to left.

(struct nf_conn *) skb->nfct 

Solution

  • I believe the point you're missing here is the Operator Precedence.

    The pointer member access operator (->) has higher precedence than the cast.

    To elaborate, (borrowed wordings)

    • Operator precedence determines which operator is performed first in an expression with more than one operators with different precedence.
    • Associativity is used (or comes into play) when two operators of same precedence appear in an expression.