-1

I came across a different way of accessing an array element today. I am just curious if its correct way to access an array element.

Here is my code,

#include <stdio.h>

int main(int argc, char* argv[])
{
    int arr[4] = {4, 5, 6, 7};
    printf ("---> %d \n", 2[arr]);
    return 0;
}

Following is the output,

---> 6

I thought the program will not compile but it did! I tried to compile it on Linux and Windows and it works fine on both. Expert views on this?

pragnesh
  • 1,240
  • 7
  • 18

1 Answers1

3

This works because for the expression a[b] is equivalent to *(a + b), which implies that you can swap the order of a and b, the addition doesn't care.

unwind
  • 391,730
  • 64
  • 469
  • 606