When you write
cout << &test.a
because test.a
is a char
, this will invoke the operator<< (ostream&, const char*)
overload, thinking that your char*
is a pointer to a C-style string rather than a pointer to just one character. Consequently, the operation will start reading bytes starting at the memory address of &test.a
until it finds a null terminator (a zero byte). This happens to print out ab@
, since a
has value 'a'
, b
has value 'b'
, and the number 478, on your system, happens to correspond to an @
character followed eventually by a null byte.
If you want to see the numeric addresses of test.a
and test.b
, cast the pointers to void*
s, which will select the operator<< (ostream&, const void*)
overload. For example:
cout << static_cast<void*>(&test.a)
Hope this helps!