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;
for(i=0;i<5;i++)
{
printf("\n%d\t%d\t%d\t%d\n",a[i],i[a],*(a+i),*(i+a));
}
return 0;
}
Click here to download the C Program.
Figure Below Shows the Output
Explanation:-
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