1

I was searching pointers in C and on a given website I found the following usage:

int main() {
    int i;
    int array[5] = {10, 20, 30, 40, 50};

    for(i = 0; i < 5; i++) {
        printf("%d\n", i[array]); /* Instead of "array[i]"" */
    }

    return 0;
}

How does it happen and why does printf function understand this?

1 Answers1

3

In C a[b] will essentially became *(a + b) so if you would do b[a] you would get *(b + a) which is the same. That is why this code works.

Eraklon
  • 4,206
  • 2
  • 13
  • 29