Skip to content
Related Articles

Related Articles

An Uncommon representation of array elements

View Discussion
Improve Article
Save Article
Like Article
  • Difficulty Level : Easy
  • Last Updated : 24 Jun, 2022

Consider the below program.  

C




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


C++




// C++ Program to represent array in an uncommon way
 
#include <iostream>
using namespace std;
 
int main() {
 
    int arr[2] = {0, 1};
    cout << "First Element = " << 0[arr] << endl;
   
    return 0;
}
 
// This code is contributed by sarajadhav12052009


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.

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!