Search code examples
coperator-precedence

(Seemingly) nonsensical operator precedence when accessing an array member of struct


A bit of an embarrassing question but I'm confused about operator precedence here:

typedef struct {
  char arr[1024];
} SomeStruct;

mySomeStruct.arr[42]

Since array access has a higher operator precedence than member access, shouldn't this actually mean mySomeStruct.(arr[42]), which just doesn't make any sense; what exactly am I trying to access in mySomeStruct? The compiler throws an error if I try mySomeStruct.(arr[42]), which checks out.

This implies that what's actually happening is (mySomeStruct.arr)[42], but doesn't this violate operator precedence?

Thanks!


Solution

  • See Eugene's comment.

    . and [] have the same precedence and are evaluated left to right.