Possible Duplicate:
C: How come an array’s address is equal to its value?
I test in GCC 4.4.1 and I find &a=a. I can’t understand it. I think &a should be the address where stores the address of the array, it can’t be the same. Could someone give me a good explanation? Thanks very much.
An array is an object in memory. It has an address and a size. It’s also true that in certain contexts, an array decays into a pointer to its first element. So numerically, if both
aand&aare compared as pointer values, they compare as equal, since they both point to the same address in memory. But they have different data types:ahas typeint[4](“array 4 ofint“), orint*(“pointer toint“) in certain contexts, whereas&aalways has typeint (*)[4](“pointer to array 4 ofint“).Hence,
(void *)a == (void *)&a.Also note that because
aand&apoint to different data types (and in particular, the pointed-to types have different sizes), doing pointer arithmetic will yield different results.a+1will point to&a[1](advances by oneintvalue), whereas&a+1will point to just past the end of the array at&a[4], since it advances by one “array 4 ofint” unit: