Search code examples
carraysoperator-precedence

Does indexing an array w/ arithmetic rather than an explicit/single value, cause issues?


a[b]

Is equivalent to *(a + b), so...

a[b & c]

Where & has a lower operator precedence than +, would this result in *(a + b & c) or *(a + (b & c))?


Solution

  • The C Standard, § 6.5.2.1, Array Subscripting says:

    The definition of the subscript operator [] is that E1[E2] is identical to (*((E1)+(E2)))

    Note the brackets surrounding E2. The latter expression (*(a + (b & c))) is the correct outcome.