0

As the title suggests, why is this valid and correct?

int main(void)
{
    int a[10];
    a[5] = 22;

    // Prints 22, similar to what a[5] would print.
    printf("%d", 5[a]);
}
mihai
  • 4,592
  • 3
  • 29
  • 42

1 Answers1

10

Three easy steps, converting the array to a pointer:

1.) a[x] == *(a+x) : An Array operation is the same as a pointer-offset addition
2.) *(a+x) == *(x+a) : Addition can be reversed
3.) *(x+a) == x[a] : pointer-offset addition can be converted back to array notation.

abelenky
  • 63,815
  • 23
  • 109
  • 159
  • Very nice how you make a flow from the popular `a[x]` to his question! And how you show how you get there! (From my perspective almost a question to protect :D ) – Rizier123 Dec 06 '14 at 14:47