Going by the books, the first cout line should print me the address of the location where the char variable b is stored, which seems to be the case for the
There is a non-member overload operator<<(std::basic_ostream) for the const char*
type, that doesn't write the address, but rather the (presumed) C-style string1). In your case, since you have assigned the address of a single character, there is no NUL
terminator, and thus no valid C-style string. The code exhibits undefined behavior.
The behavior for int*
is different, as there is no special handling for pointers to int
, and the statement writes the address to the stream, as expected.
If you want to get the address of the character instead, use a static_cast
:
std::cout << static_cast( c ) << std::endl;
NUL
character ('\0'
).