44
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?

2
  • 5
    ¤ The only difference between t converted to pointer, and &t, is the pointer type. The latter's referent is formally of array type, so that adding 1 to it moves it quite a bit (to the next such array in memory). While the former's referent is of the element type. Terminology: out in the [comp.lang.c++] Usenet group there was some controversy about whether a pointer like your u could be said to "point to" the array, since its referent is not of array type. That was resolved umpteen times by pointing to the standard's use of such wording. Cheers & hth., Commented Dec 7, 2011 at 9:10
  • &u is a reference to the integer pointer, you normally use this as the argument of some function. Commented Apr 18, 2017 at 0:15

3 Answers 3

44

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.

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks! I suppose it was the implicit array-to-pointer conversion that gave me the wrong mindset of u being a pointer.
@quuxbazer: u is a pointer. t is an array. int * u = t applies an array-to-pointer conversion to t, and initialises u with the result.
@JohannesSchaub-litb: I would be interested in the correct description in that case.
(e ? a : n) does not produce a pointer if both sides have the same array type for example.
@JohannesSchaub-litb: I don't see how that invalidates this answer. Sure, there are some situations when array-to-pointer conversions do not take place. However, in this case they are occurring.
|
5

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.

Comments

-4

There is no variable called t, since you can't change it. The name t simply refers to the address of the first element (and also has a size associated with it). Thus, taking the address of the address doesn't really make sense, and C "collapses" it into just being the address.

The same sort of thing happens for the case of functions:

int foo(void)
{
  return 12;
}

printf("%p and %p\n", (void *) foo, (void *) &foo);

This should print the same thing, since there is no variable holding the address of foo, whose address in turn can be taken.

2 Comments

It's C++, and there is variable called t. You can pass it to function which accepts argument with type int(&)[10].
-1 "There is no variable called t, since you can't change it." is incorrect. An object that is named in a declaration is called a variable. Go look up the standard.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.