Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Address of an array

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?

like image 600
quuxbazer Avatar asked Dec 07 '11 09:12

quuxbazer


People also ask

How do I find the address of an array element?

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 / ...

Does an array have an address?

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.

What is the memory address of an array in C?

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.


2 Answers

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.

like image 101
Mankarse Avatar answered Oct 05 '22 18:10

Mankarse


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.

like image 21
Abyx Avatar answered Oct 05 '22 18:10

Abyx