#include<iostream>
#include<string>
usingnamespace std;
int main(){
int a = 10 ;
cout<<"a is : "<< a <<endl<<"&a is : "<<&a;
char b = 'f';
string g = "d";
cout<<endl <<"b is : "<< b <<endl<<"&b is : "<<&b
<<endl<<"g is : "<<g <<endl<<"&g is : "<<&g<<endl;
return 0;
}
std::cout << &b attempts to print a NTBS (null terminated byte string, a sequence of characters in an array), where the address of the first character to be printed is given by &b.
To print out the actual address of a character, convert the pointer to char to pointer to const void; there is no special treatment for pointer to void, the value of the pointer is printed out.
1 2 3 4 5 6 7 8
#include <iostream>
int main() {
char b = 'f';
constvoid* pv = &b ; // char* implicitly converted to const void*
std::cout << "b is : " << b << '\n' <<"address of b is : "<< pv << '\n' ;
}
Repeat: To print out the actual address of a character, convert the pointer to char to pointer to const void
This is what the standard guarantees:
A prvalue of type “pointer to cv T ”, where T is an object type, can be converted to a prvalue of type “pointer to cv void ”. The result of converting a non-null pointer value of a pointer to object type to a “pointer to cv void ” represents the address of the same byte in memory as the original pointer value