Arrays used in expressions with rare exceptions are converted to pointers to their first elements.
From the C++ Standard (4.2 Array-to-pointer conversion)
1 An lvalue or rvalue of type “array of N T” or “array of unknown
bound of T” can be converted to a prvalue of type “pointer to T”. The
result is a pointer to the first element of the array.
So in this statement
std::cout << *array << std::endl;
the sub-expression array
has the type char **
because elements of the array have the type char *
.
Thus the expression *array
has the type char *
. And you are trying to output a string pointed to by the pointer *array
that is not zero-terminated. So the program has undefined behaviour. It outputs all characters following the character 'a'
until a zero character is encountered in the memory.
You need to write
std::cout << **array << std::endl;
to output just one character.
Here is a demonstrative program
#include <iostream>
int main()
{
const size_t SIZE = 2;
char w = 'a';
char *array[SIZE];
array[0] = &w;
std::cout << **array << std::endl;
return 0;
}
Its output is
a
This statement
std::cout << *array << std::endl;
would work fine if after the character 'a' in your object there will be a zero-character that is if the statement would deal with a string.
For example
#include <iostream>
int main()
{
const size_t SIZE = 2;
char w[] = { 'a', '\0' };
char *array[SIZE];
array[0] = &w[0];
std::cout << *array << std::endl;
return 0;
}
The program output is the same as shwon above.