1

Today when i was coding inside my visual studio i unintentionally did following

for(int i=0;i<10;i++)
{
    cout<<"Value is"<<[i]arr<<endl;
}

instead of arr[i] and it worked.why it worked?

user3266922
  • 73
  • 1
  • 7
  • 2
    Because that's a totally valid syntax for indexing an array. – user229044 Feb 05 '14 at 17:05
  • see: http://stackoverflow.com/questions/5073350/accessing-arrays-by-indexarray-in-c-and-c – Csq Feb 05 '14 at 17:06
  • 1
    It's not *incorrect*, it's just semantically unclear. Well, silly, anyway. It's the kind of thing that interviewers and professors like to discuss, and will get you shot in a code review. – John Dibling Feb 05 '14 at 17:12

1 Answers1

7

Because [i]arr == *(i + arr) == arr[i]

Note: + operator holds commutative property

Sunil Bojanapally
  • 12,528
  • 4
  • 33
  • 46