Okay, consider following piece of code in C:
int main(int argc, char *argv[])
{
int arr[10];
arr; // This has an address to the first element of arr[10] and is of type int *
&arr; // Has same value as arr but the type is a pointer to an array of [10] ints. Also pointer arithmetic here would be different as compared to arr.
int var; //var is a variable and has an address. &&var --> Doing this would be invalid. If so, then what is arr? Why is &arr allowed?
return 0;
}
I know the difference between above two expressions but what I am having a hard time understanding is why these two have the same value? So I have following two questions regarding this:
- Considering if
arr[0]
has an arbitrary location of0x100
, then whyarr
and&arr
has the same value? How exactly does compiler treat these two expressions and how would they be really implemented in memory? - What exactly is the use of having
&arr
in C? Since,arr
is already an address, why is&
operator even allowed on it?
EDIT 1: Removed reference to ptr
& ptr2Array
as people were misinterpreting the question.