top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Is a[5] == 5[a]? If yes why?

+3 votes
110 views
Is a[5] == 5[a]? If yes why?
posted Jan 22, 2015 by Khusboo

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

+4 votes
 
Best answer

In C, Array can be represented as pointer and vice versa.

so,
instead of a[5] we can also write *(a+5).
instead of 5[a] we can also write *(5+a).

so, a[5] == 5[a]

answer Jan 23, 2015 by Chirag Gangdev
You got it right.
+2 votes
main()
{
   int a[]={1,2,3,4,5,6,7,8};

   if (a[5] == 5[a])
     printf("Equal");
}

Now come on the explanation -
a[5] means *(a+5) and 5[a] means *(5+a) and we know both are same.

answer Jan 22, 2015 by Salil Agrawal
...