An Uncommon representation of array elements

Consider the below program.

filter_none

edit
close

play_arrow

link
brightness_4
code

int main( )
{
  int arr[2] = {0,1};
  printf("First Element = %d\n",arr[0]);
  getchar();
  return 0;
}

chevron_right


Pretty Simple program.. huh… Output will be 0.

Now if you replace arr[0] with 0[arr], the output would be same. Because compiler converts the array operation in pointers before accessing the array elements.

e.g. arr[0] would be *(arr + 0) and therefore 0[arr] would be *(0 + arr) and you know that both *(arr + 0) and *(0 + arr) are same.

Please write comments if you find anything incorrect in the above article.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up
Article Tags :
Practice Tags :


23


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.