1

I am using Ubuntu 12.04lts with the GCC compiler. This program gives the result 10. Could you anybody please describe why this program gives the result like this?

 #include <stdio.h>
 void main(void)
 {
     int arr[1] = {10};
     printf("\n%d\n\n", 0[arr]);
 }
Jonathan Leffler
  • 666,971
  • 126
  • 813
  • 1,185

2 Answers2

5

arr[0] gets internally expanded to *(arr+0). Similarly 0[arr] gets expanded to *(0+arr) which points to the same thing. Hence you see 10.

In general for an array or a pointer a, a[b] always means *(a+b) where a is the starting address of the array or pointer and b is the offset. Thus, a[b] and b[a] are equivalent.

digital_revenant
  • 3,194
  • 1
  • 11
  • 24
0

below line means arr is int type array and it has size 1 and it is initialized with 10 ie index 0 has 10

int arr[1] = {10};

then next line printf statement printing the value of arr at index 0.

printf("\n%d\n\n",0[arr]);

Rocker
  • 1,217
  • 7
  • 15