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,329
  • 3
  • 27
  • 41

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
  • 61,055
  • 22
  • 102
  • 154
  • 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