The subscript operator has the second highest operator precedence (see the table). However, it seems to behave as if it had a low precedence. For example:
int arr[] = {10,20,30,40,50};
cout << arr[1+2];
This code outputs 40, which suggests that the result of +
was available before the application of the subscript operator, which in turn suggests that +
has a higher precedence than the subscript operator. What am I missing and how does the high precedence of the subscript operator express itself?
The subscript operator []
is applying to arr
and the argument is 1 + 2. The argument is not relevant to the precedence of the []
operator.
Cf. an expression like 1 + arr[1 + 2]
. That is grouped as 1 + (arr[1 + 2])
due to the precedence that you cite, not (1 + arr)[1 + 2]
which would be some curious pointer arithmetic!