May 22, 2004 2:18 PM
Pointers and arrays are intimately related. An array name is automatically the base address of the array which implies an array can be accessed via pointers as well. With this in mind, consider the following block of C code:

int main()
  int arr[5]={100,200,300,400,500};
  int a;
    printf("%d:%d ",a+1,a[arr]);
  return 0;

Wondering whether this code will compile? Once that doubt has been cleared, consider the output:

1:100 2:200 3:300 4:400 5:500

The key idea behind the interchanging of the index and the array name is that ultimately arrays are accessed using the formula:

*(arrayname + index)

Considering the C snippet and the above formula, you must agree with me that since addition is commutative, *(arrayname + index) is same as *(index + arrayname). Therefore, a[arr] and arr[a] returns the same element in the array arr.


Copyright © 2004-2011 Anirudh Sasikumar. All rights reserved.
Last Updated: January 21, 2005 4:37 PM