Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
89 views
in Technique[技术] by (71.8m points)

c++ - 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?

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...