Search code examples
cexpressionoperator-precedence

brackets and points order in language c


I wonder in language c, is a.b[i] is the same thing as (a.b)[i] ?

Here a is a structure and b is his member (b is an array or a pointer).
I read an article about precedence of c operators just now and it told me that the [ ] (brackets) operator is prior to the .(point), and the associativity of these two c operators is left-to-right :

So what is the order of the following expression (in c)?

a.b.c[i].d[j]

Solution

  • Operators . and [] have the same precedence, and are evaluated from left-to-right. The expression a.b[i] is the same as (a.b)[i]

    The expression a.b.c[i].d[j] is evaluated as: ((((a.b).c)[i]).d)[j]