int t[10]; int * u = t; cout << t << " " << &t << endl; cout << u << " " << &u << endl;
Output:
0045FB88 0045FB88 0045FB88 0045FB7C
The output for u
makes sense.
I understand that t
and &t[0]
should have the same value, but how come &t
is also the same? What does &t actually mean?
In a single dimensional array the address of an element of an array say A[i] is calculated using the following formula Address of A[i]=B+W∗(i–LB) where B is the base address of the array, W is the size of each element in bytes, i is the subscript of an element whose address is to be found and LB is the Lower limit / ...
Each element of an array has own address. Address of an array is the address of it's first element. Show activity on this post. In C++, an object is some bytes in memory.
When a variable is created in C, a memory address is assigned to the variable. The memory address is the location of where the variable is stored on the computer. When we assign a value to the variable, it is stored in this memory address.
When t
is used on its own in the expression, an array-to-pointer conversion takes place, this produces a pointer to the first element of the array.
When t
is used as the argument of the &
operator, no such conversion takes place. The &
then explicitly takes the address of t
(the array). &t
is a pointer to the array as a whole.
The first element of the array is at the same position in memory as the start of the whole array, and so these two pointers have the same value.
The actual type of t
is int[10]
, so &t
is the address of the array.
Also, int[]
implicitly converts to int*
, so t
converts to the address of the first element of the array.
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