Here is a code which I wrote to test/understand the behavior of pointers of/in an array
int main(void){
int a[4];
memset(a, 0, sizeof(a));
printf("%x %x\n",a,&a);
}
Output of the above program on my machine:
bfeed3e8 bfeed3e8
I can't understand why are the values a and &a is same. From what I understand &a is supposed to give the address of memory location where a is stored. What is the explanation for this kind of behavior?
&a
does give you the address of a
. a
gives you a
, which, because it's an array, decays to a pointer to a
.
Or to be pedantic, it decays to a pointer to a[0]
.
In that context, the array name a
decays into a pointer to its first element. Did you mean
printf("%x %x\n",a[0],&a);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With