The problem lies in the overload of operator<<
for std::ostream
. The specialization that prints ascii characters uses const char *
, and you are trying to print char[][]
, which will decay into a type that's going to be printed as a "generic pointer" (void const *
to be precise) - char(*)[2]
, or "a pointer to two-element array of char
(thanks to @Wintermute for that).
std::array<std::string, 2> a { "A", "B" };
is probably the most idiomatic solution here.
If you don't want to modify the contents, std::array<const char*, 2>
will also do just fine; const char*
is the type of string literals. If you store them that way, you're actually storing the addresses to where the compiler puts them when building the binary. This memory isn't modifiable by you, that's why you have to use const
. The string
variant copies the data, when that place of code is encountered, to a part of memory you have write access to.
Of course, when you choose the proper way, the actual printing needs to be changed, too!
for (auto const& str : a) {
cout << str << endl;
}
Oh and also, you probably don't want to use using namespace std;
.