I had the following code :
#include<stdio.h>
void main()
{
int * a;
int arr[2];
arr[1] = 213 ;
arr[0] = 333 ;
a = &arr ;
printf("\narr %d",arr);
printf("\n*arr %d",*arr);
printf("\n&arr %d",&arr);
printf("\n%d",a[1]);
}
On running this simple program i get the output as follows :
arr -1079451516
*arr 333
&arr -1079451516
213
Why is it that both arr and &arr give the same result ? I can understand that arr is some memory location and *arr or arr[0] is the value stored at the position, but why is &arr and arr same ?
Almost any time you use an expression with array type, it immediately “decays” to a pointer to the first element.
arrbecomes a pointer with typeint*, and this pointer is what’s actually passed toprintf.&arris a pointer with typeint (*)[2](pointer to array of twoints). The two pointers have the same address, since they both point at the beginning of the array.(One notable exception to the array-to-pointer conversion is in a
sizeofargument.)