Why does cout print char arrays differently from other arrays?
It's the operator<< that is overloaded for const void*
and for const char*
. Your char array is converted to const char*
and passed to that overload, because it fits better than to const void*
. The int array, however, is converted to const void*
and passed to that version. The version of operator<< taking const void*
just outputs the address. The version taking the const char*
actually treats it like a C-string and outputs every character until the terminating null character. If you don't want that, convert your char array to const void*
explicitly when passing it to operator<<:
cout << static_cast<const void*>(arr2) << endl;
Because cout's operator <<
is overloaded for char*
to output strings, and arr2
matches that.
If you want the address, try casting the character array as a void pointer.