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;