To stuff something into an output-stream (std::ostream
) like std::cout
, the stream insertion operator <<
is used:
std::cout << "Hello, World!";
The stream insertion operator that is called for string literals like "Hello, World"
looks like
std::ostream& operator<<(std::ostream& os, const char* s);
As you can see, the 2nd parameter is a pointer to const char
. Now if you would write
char givenChar;
std::cout << &givenChar;
the address-of operator &
would give you the address of givenChar
. The type of this address is char*
which is convertible into a char const*
. So the above mentioned function
std::ostream& operator<<(std::ostream& os, const char* s);
would be called (like operator<<(std::cout, &givenChar)
) which would interpret the memory at the location of the address of givenChar
as a zero-terminated string. Eg. it would read from the memory until it finds a '\0'
. But at the address of givenChar
is only space for *one* char
which most likely is not zero. This would result in garbage inserted into std::cout
(=printed) and eventually lead to an access violation.
So instead you use
char givenChar;
std::cout << (void*) &givenChar;
(void*)
is a cast. It converts the char*
produced by applying the address-of operator &
to the char
givenChar
into a pointer to void
. For a void*
the operator
ostream& operator<<(void* val);
gets called which will only insert the numeric value of the given address into the stream instead of trying to print a string that might exist at the address.