The code i wrote was a bit odd. I put
(char *)
simply for comparison purposes. In fact if that part is omitted, the behaviour is exactly the same, since the variable was already a pointer to char.
It didn't work because, as Zhuge said, cout is overloaded to presume char*'s are C-style strings and print the contents rather than the address.
The word "overloaded" here is important. It refers to a function which has multiple versions with the same name but a different type of parameter.
For example, you could have two functions called add:
1 2
|
int add(int x, int y) { return x + y ; }
double add(double x, double y) { return x + y ; }
|
When you call the function, the compiler chooses which one to call, depending upon the type of parameter.
In the case of
cout <<
if the parameter is a pointer to char, the compiler assumes this is a character string, and so tries to print out the string.
If the parameter is a pointer to
void
(that is, a general pointer to no specific type), the compiler chooses a different version, which prints the value of the pointer itself.