-1

Today i have learned about pointers and i have this:

char message[]="hello everyone";
char *pm;
pm=message;
cout<<&pm[0];

This would output hello everyone, I want to know why? because & should output the address of pm[0] not the hole value. Can someone explain?

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
  • 1
    `&pm[0]` is the same as `pm`, which is the same as `message`. If you `cout << message` you would get `hello everyone` and yet you aren't surprised by it. The answer is that operator << is overloaded to print the whole character array if it takes a pointer to a const char. – Noel Dec 04 '14 at 11:10
  • 1
    @NoelPerezGonzalez `pm` is not the same as `message`. `message` is an array whereas `pm` is a pointer. –  Dec 04 '14 at 11:12
  • @rightføld true, I meant pm contains the same info as message, that is, the address of the first element of the array. – Noel Dec 04 '14 at 11:18
  • 2
    @NoelPerezGonzalez: Please do not spread the horrible myth and rumour that arrays and pointers are the same thing; `message` and `pm` are _absolutely_ not the same thing. Your example about `cout << message` has more to do with array name decay and less to do with some imaginary "arrays are pointers" nonsense. – Lightness Races in Orbit Dec 04 '14 at 11:21
  • pm[0] represent the char value at index 0. &pm[0] represent its address. So here &pm[0] == pm == message. If we pass char* to the << operator, it output null terminated string. – Vishal Gupta Dec 04 '14 at 11:25

4 Answers4

4

&pm[0] is of type char*. << is overloaded for char const* to print all characters until '\0'. If you want to print the address you have to convert it to void* first.

R. Martinho Fernandes
  • 228,013
  • 71
  • 433
  • 510
2

message is an array of chars containing hello everyone\0.

When you do pm = message;, the array message undergoes array-to-pointer conversion, which gives you a pointer pointing at the first element in the array. So pm now points at the very first char in the array (the h character).

pm[0] is equivalent to *(pm + 0), which basically just gives you the first character in the array. Then &pm[0] is taking the address of that character, giving you a pointer to the first character. So pm and &pm[0] are exactly the same.

cout has a special overload for handling char*s. When you pass a char* to cout, it assumes that you want it to print it out like a C-style string, where the char* is pointing at the first character in an array of characters that end with \0.

Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324
1

&pm[0] is the same as &(*(pm+0)) which is the same as &(*pm) which is the same as pm. And pm points to the first element of the zero-terminated character array message.

Now, the << operator for the output stream, when passed a char* or const char* treats that as a pointer to a null-terminated array of characters. That is, a C string.

So, the entire contents of the string are output to the output stream.

& should output the address of pm[0]

No, the & operator is not related to the output. The & operator is part of the expression &pm[0]. This expression is evaluated, as described above. And the result of that evaluation is passed to the best matching overload of the output stream's << operator. It is the << operator that determines how the value of your expression is output, and that determination is based on the type of your expression.

In order to output an address, you would need to supply an argument of a different type, for instance void*. You can cast the char* pointer like this:

static_cast<const void*>(&pm[0])
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
-1

Usually, giving a pointer to std::cout will result in the pointer's value (the memory address it contains) being printed.

However, there is a special case for [const] char*, which is automatically assumed to be a C-style string and therefore dereferenced to be treated like one. This (in concert with array name decay) is to allow you to do things like std::cout << "Hello world\n"; without just getting a memory address instead.

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