As you know, pointers are the variable which stores the address of another variable or pointer.
Consider this basic one dimension array ' arr[ ] '
int arr[ ] = { 10 , 20 , 30 , 40 , 50 } ;
printf ( " %d %d ", *arr , arr[0] );
The output to this program is 10 10.
When mentioning the name of the array 'arr' we get its base address. Thus *arr refers to the zeroth element of the array, i.e , 10.
similarly
int arr[ ] = { 10 , 20 , 30 , 40 , 50 };
for(int i=0 ; i<5 ; i++ )
printf ( " %d %d \n" , *( arr + i ) , arr[i] );
We get the output as
10 10
20 20
30 30
40 40
50 50
When we say, arr[i] , the C compiler internally converts it to *( arr + i ) . Hence this means that the following notations are same:
arr [ i ] = *( arr + i ) = *( i + arr ) = i [ arr ]
To prove this compile the below program:
int main( ) {
int arr[ ] = { 10 , 20 , 30 , 40 , 50 };
int i;
for( i=0 ; i < 5 ; i++ ){
if ( arr[i] = = i[arr] )
printf(" True ");
else
printf(" False ");
}
return 0;
}
------
Categories:
Program concepts