Sunday, 3 March 2013

C/C++ Puzzles: PART - 25

Peculiar behavior of Arrays

Consider the program given below. The program prints the array elements in different ways. How is it possible? 


int main( )
    int a[ ]={1,2,3,4,5};
    int i;
   return 0;

Click here to download the C Program.

Figure Below Shows the Output


  The content of an array a[i] is calculated as follows. *(Starting Address  + Array Index ), where a is the starting address and i is the array index. Since addition is a commutative operation a+b and b+a are the same, so a[i], i[a], *(a+i) and *(i+a) all returns the ith element of the array a.   

No comments:

Post a Comment