-3

I have come across a c++ code:

char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
cout << "Greeting message: ";
cout << greeting << endl;

output: Hello

Since greeting is an array of size 6, displaying greeting should display only "H" because of greeting[0] in cout, since it is displaying first address of array. I don't know where I am wrong.

Ali Seyedi
  • 1,758
  • 1
  • 19
  • 24
  • 3
    Please clarify why you expect it to display only "H". You are printing `greeting` not `greeting[0]` – kmdreko Apr 01 '16 at 16:56
  • 1
    `greeting` decays into a pointer `char *` and the entire string it is pointing to will be printed. Use `cout << greeting[0] << endl;` if you only want to print the first char. – Unimportant Apr 01 '16 at 16:57
  • this is how cout display a character string...Because greeting is a string that's why cout display "Hello" but if you write "greeting[0]" then cout will display 'H' as a character... – Shiv Apr 01 '16 at 16:58
  • The `operator <<` for `char *` types is overloaded to start at the given address and print characters until a `\0` is found. – Thomas Matthews Apr 01 '16 at 16:58
  • isnt greeting representing the first value of array? – Prashant Singh Apr 01 '16 at 17:00
  • it's not *representing* the first value of array. The name of the array *decays* to *the pointer* to the first element of the array. – unkulunkulu Apr 01 '16 at 17:01
  • http://stackoverflow.com/questions/1461432/what-is-array-decaying read to get a better understanding of whats going on – Jason Ball Apr 01 '16 at 17:02
  • @PrashantSingh: No. Where did you hear that? – Lightness Races in Orbit Apr 01 '16 at 17:04
  • its due to not listening to lectures properly :p – Prashant Singh Apr 01 '16 at 17:06
  • @PrashantSingh: or your teacher may have explained it incorrectly, which is sadly common. A *lot* of people get this very simple concept wrong. – John Bode Apr 01 '16 at 17:10
  • `greeting` is not the same as `greeting[0]` it's `*greeting` that is the same, i.e. `greeting` (the array name) points to the first element basically. – unkulunkulu Apr 01 '16 at 17:17

4 Answers4

7

Except when it is the operand of the sizeof or unary & operators, an expression of type "N-element array of T" will be converted ("decay") to an expression of type "pointer to T", and the value of the expression will be the address of the first element of the array.

This means that in the statement

cout << greeting << endl;

the expression greeting is converted from an expression of type char [6] to an expression of type char *, and the value of the expression is the address of the first element.

The stream operator << is defined such that if it receives an argument of type char *, it will write out the sequence of characters starting at that address until it sees the 0 terminator; here's a simplistic example of how it might work:

std::ostream& operator<<( std::ostream& s, char *p )
{
  while (*p)
    s.put( *p++ );
  return s;
}

The real operator definition will be a bit more complex, but that's the basic idea.

If you want to print out just the first character of greeting, you must explicitly index or dereference it:

cout << greeting[0] << endl;

or

cout << *greeting << endl;
John Bode
  • 119,563
  • 19
  • 122
  • 198
2

greeting decays to a pointer itself. But if it sounds complicated, you can look at it as a pointer to understand what cout does.

cout method prints characters starting from greeting[0] until \0.

Community
  • 1
  • 1
Ali Seyedi
  • 1,758
  • 1
  • 19
  • 24
1

If you want to see the address value write

char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
cout << "Greeting message: ";
cout << (void*)greeting << endl;
     // ^^^^^^^ 

The std::ostream& std::operator<<(std::ostream&, T type); provides specializations for the const char*, const char[] and char types.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
0

yes greeting here is a pointer to first element in the array so if you do something like that : cout << *greeting; output will be H. but in reality when you pass it to cout object it's smart enough to know that greeting is not just a pointer but it's more it's a string according to the internal implementation of operator overloading in handling . so it's not a problem ,it's just cout can understand it.