Right Click to Search

Friday, August 6, 2024

How Array Element is randomly accessed ? [ Real Fact : *(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

Tags / Keywords : |

Stumble
Delicious
Technorati
Twitter
Facebook

0 Comments:

Post a Comment

Your Feedback :This is Growing Site and Your Feedback is important for Us to improve the Site performance & Quality of Content.Feel Free to contact and Please Provide Name & Contact Email

 

Learn C Programming Copyright © 2010 LKart Theme is Designed by Lasantha, Free Blogger Templates