a[7]
is equivalent to *(a + 7)
.
- Similarly
7[a]
is equivalent to *(7 + a)
.
This gives the same result because adding an integer to a pointer is a commutative operation.
is there any case that it can be false
The short answer is no. For simple pointer arithmetic it will always be true.
The long answer is that to find a counterexample you would have to use a hack. One way you could do it is to use a macro:
#include <stdio.h>
#define a 1+s
int main(void) {
char s[] = "1q2w3e4r5t6y";
int x = a[7];
int y = 7[a];
printf("%c %c\n", x, y);
return 0;
}
Result:
s 5
The result is different because after the macro expansion the expressions become different: 1+(s[7])
versus 7[1+s]
.
See it online: ideone