0

Im reading a book in c++ and I was doing some tests as I didnt understand something and when I tried printing these 3 lines I was expecting something different for all 3. The first one, the location in memory of the pointer, the second one, the address stored by the pointer, and the third one, the value at the address stored by the pointer. But instead the first two were the same:

cout <<&arey <<"\n";
cout <<arey <<"\n";
cout <<*arey <<"\n";

output:

0x6afee8
0x6afee8
0

Can someone explain why as I have read that the name of the array is a pointer to the array so if it is a pointer it should have its own memory address right?

  • 1
    Also related: https://stackoverflow.com/questions/2528318/how-come-an-arrays-address-is-equal-to-its-value-in-c – scohe001 Feb 28 '18 at 19:02
  • 1
    I mean no offense by posting these links, I'm only trying to save someone else the effort of answering this question if it's already been answered. Your question seems to boil down to "*why are `arey` and `&arey` outputting the same thing*?" which I believe my linked questions to answer. If you don't think so, please feel free to comment why your question is different so that answers can focus on that! – scohe001 Feb 28 '18 at 19:06
  • 1
    That being said, while those are helpful links I do not believe either is an appropriate duplicate. – Lightness Races in Orbit Feb 28 '18 at 19:07

2 Answers2

5

I dont understand how an arrays name is a pointer to the array

It isn't.

I have read that the name of the array is a pointer to the array

Whoever wrote that was lying to you.

The name of the array is the name of the array.

Can someone explain why

Under certain conditions (e.g. passing it to a function) it gets automatically (and transparently) converted to a pointer to the array's first element. That's an unfortunate truth we inherited from C.

But the name of the array is still just the name of the array.

So:

arey          An array
&arey         Address of the array
f(&arey)      Passing the address of the array
f(arey)       Still passing the address of the array (ish) - thanks a lot, C!
f(*arey)      Passing the first element's value

Now consider that cout << x is actually cout.operator<<(x) (i.e. a function call) and that explains your output.

if it is a pointer it should have its own memory address right?

If it were a pointer then, yes, it would.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
2

Addendum to the answer of Lightness Races in Orbit:

Because this question is categorized as a C++ question and not as plain C, there might be the following of interest, too.

Modern C++ suggests to use std::array in favor of a plain C-Array. It comes without costs, but more comfort (like combining location in memory and size or even compile-time bounds checks).

See also: std::array and C++ Core Guidelines - std::array

Most operations are transparent to the usage of C-Arrays.

Additionally it is to prefer to use std::addressof in favour of & (which also works well with std::array).

See: std::addressof

cwschmidt
  • 1,194
  • 11
  • 17