Array Randomly Accessed : Real Fact behind *(a+i)

How Array Element is randomly accessed ?

  1. Array elements are randomly accessed.
  2. Array Can be accessed using Array-Name and Subscript Variable written inside pair of Square Brackets [].
  3. Example -
arr[3]  = Third Element of Array
arr[5]  = Fifth Element of Array
arr[8]  = Eighth Element of Array

How Elements gets Assigned ?

arr[0]  = 51
arr[1]  = 32
arr[2]  = 43
arr[3]  = 24
arr[4]  = 5
arr[5]  = 26

Real Thing ? How a[4] Works ????
We know that array is declared like this -

int arr[] = { 51,32,43,24,5,26};
  • So already we have base address of Array
  • Consider *arr will gives Zeroth Element from Array.
  • Similarly *(arr+0) will also yields same result.

Note :

Generally Accessing a[i] means retrieving element from address (arr + i) .

Observations and Conclusion :

arr[i]    = 5
*(arr+i)  = 5
*(i+arr)  = 5
i[arr]    = 5

all of the above notations yields same result.

Let’s Prove it -

#include< stdio.h>
#include< conio.h>
void main()
{
int arr[] = { 51,32,43,24,5,26};
int i;
    for(i=0;i<=5;i++)
         printf("\n%d %d %d %d",arr[i],*(i+arr),*(arr+i),i[arr]);
getch();
}

Output :

51 51 51 51
32 32 32 32
43 43 43 43
24 24 24 24
5 5 5 5
26 26 26 26