Why are `&array` and `array` pointing to the same address?
Plain array
decays to a pointer to its first element, it's equal to &array[0]
. The first element also happens to start at the same address as the array itself. Hence &array == &array[0]
.
But it's important to note that the types are different:
- The type of
&array[0]
is (in your example)int*
. - The type of
&array
isint(*)[5]
.
The relationship between &array[0]
and &array
might be easier if I show it a little more "graphically" (with pointers added):
+----------+----------+----------+----------+----------+
| array[0] | array[1] | array[2] | array[3] | array[4] |
+----------+----------+----------+----------+----------+
^
|
&array[0]
|
&array
As an extra addendum, array
decays to a pointer to its first element, that is array
decays to &array[0]
and will thus have the same type.
Things are different with pointers though. The pointer pArray
is pointing to some memory, the value of pArray
is the location of that memory. This is what you get when you use pArray
. It is also the same as &pArray[0]
.
When you use &pArray
you get a pointer to the pointer. That is, you get the location (address) of the variable pArray
itself. Its type is int**
.
Somewhat graphical with the pointer pArray
it would be something like this
+--------+ +-----------+-----------+-----------+-----------+-----------+-----+
| pArray | ----> | pArray[0] | pArray[1] | pArray[2] | pArray[3] | pArray[4] | ... |
+--------+ +-----------+-----------+-----------+-----------+-----------+-----+
^ ^
| |
&pArray &pArray[0]
[Note the ...
at the end of the "array", that's because pointers retains no information about the memory it points to. A pointer is only pointing to a specific location, the "first" element of the "array". Treating the memory as an "array" is up to the programmer.]
Why the address of pointer of an Array is the same of the data stored in that pointer?
printing p
means print p
's value, while p
's value is address of a integer number or address of an integer array.
printing &p
means you print location of p
on memory.
printing arr
will show address of first element of the array, it is &arr[0]
.
printing &arr
will show location of arr on memory, it is also adress of first element of the array
So printing p
will be different with printing &p
.arr
and &arr
are different types but it will give you the same result.
Why in a 2D array a and *a point to same address?
A visual way to understand the 2D array is like this...
a --> a[0] --> [1,2,3]
a[1] --> [4,5,6]
a[2] --> [7,8,9]
a[0], a[1], a[2] are logical representation and not actual memory.
so a[0] = a + 0*[bytes occupied by one row],
a[1] = a + 1*[bytes occupied by one row],
a[2] = a + 2*[bytes occupied by one row]
Hence *a => a[0] => a
Note that even though the address is same the "type" of pointer changes when we invoke a
vs a[0]
or *a
. This can be easily seen by your print output of (a+1) and (a[0]+1).
How come an array's address is equal to its value in C?
The name of an array usually evaluates to the address of the first element of the array, so array
and &array
have the same value (but different types, so array+1
and &array+1
will not be equal if the array is more than 1 element long).
There are two exceptions to this: when the array name is an operand of sizeof
or unary &
(address-of), the name refers to the array object itself. Thus sizeof array
gives you the size in bytes of the entire array, not the size of a pointer.
For an array defined as T array[size]
, it will have type T *
. When/if you increment it, you get to the next element in the array.
&array
evaluates to the same address, but given the same definition, it creates a pointer of the type T(*)[size]
-- i.e., it's a pointer to an array, not to a single element. If you increment this pointer, it'll add the size of the entire array, not the size of a single element. For example, with code like this:
char array[16];
printf("%p\t%p", (void*)&array, (void*)(&array+1));
We can expect the second pointer to be 16 greater than the first (because it's an array of 16 char's). Since %p typically converts pointers in hexadecimal, it might look something like:
0x12341000 0x12341010
Array base pointer and its address are same. Why?
There is no "array base pointer", i.e. there no pointer variable that points to the array. The name of the array refers to the array itself. Therefore you can not take an address of a
, instead, &a
is handled as a special case.
When you use the name of an array in an expression, it decays into a pointer that points to the first element of the array. However, &
and sizeof
operators are exceptions.
ANSI C specifies that &a
means the address of the array itself, and its type is "pointer to array", not "pointer to array element".
In pre-ANSI compilers, &a would cause a warning.
Why is address of an array variable the same as itself?
the fact that
a
can never point to any other location
This isn't a fact, though. If a
is an array, a
doesn't point anywhere because a
is not a pointer. Given int a[42];
, a
names an array of 42 int
objects; it is not a pointer to an array of 42 int
objects (that would be int (*a)[42];
).
&x
gives you the address of the object x
; if x
is an array type variable, then &x
gives you the address of the array; if nothing else, this is consistent with the behavior of &
for any other object.
A better question would be "why does an array (like a
) decay to a pointer to its initial element in most cases when it is used?" While I don't know with certainty why the language was designed this way, it does make the specification of many things much simpler, notably, arithmetic with an array is effectively the same as arithmetic with a pointer.
Why is address of an array variable the same as itself?
the fact that
a
can never point to any other location
This isn't a fact, though. If a
is an array, a
doesn't point anywhere because a
is not a pointer. Given int a[42];
, a
names an array of 42 int
objects; it is not a pointer to an array of 42 int
objects (that would be int (*a)[42];
).
&x
gives you the address of the object x
; if x
is an array type variable, then &x
gives you the address of the array; if nothing else, this is consistent with the behavior of &
for any other object.
A better question would be "why does an array (like a
) decay to a pointer to its initial element in most cases when it is used?" While I don't know with certainty why the language was designed this way, it does make the specification of many things much simpler, notably, arithmetic with an array is effectively the same as arithmetic with a pointer.
Related Topics
Explicit Return Type of Lambda
Can Branches with Undefined Behavior Be Assumed Unreachable and Optimized as Dead Code
Efficiently Reading a Very Large Text File in C++
Std::Lexical_Cast - Is There Such a Thing
Enum VS Constexpr for Actual Static Constants Inside Classes
Restrict Variadic Template Arguments
How to Get File Extension from String in C++
Why Use !! When Converting Int to Bool
Pass by Pointer & Pass by Reference
Does Try-Catch Block Decrease Performance
Why Sizeof Int Is Wrong, While Sizeof(Int) Is Right
Is Make_Shared Really More Efficient Than New
How to Use a Custom Type as Key for a Map in C++
Getting an Array of Bytes Out of Windows::Storage::Streams::Ibuffer