Search code examples
c++charcharactermemory-address

The address of character type variable


Here is just a test prototype :

#include <iostream>
#include <string>
using namespace std;

int main()
{
   int a=10;
   char b='H';
   string c="Hamza";

   cout<<"The value of a is : "<<a<<endl;
   cout<<"The value of b is : "<<b<<endl;
   cout<<"The value of c is : "<<c<<endl<<endl;

   cout<<"address of a : "<<&a<<endl;
   cout<<"address of b : "<<&b<<endl;
   cout<<"address of c : "<<&c<<endl;

   return 0;
}

Why the address of variable 'b', which is of character type, not printing?


Solution

  • The << operator in your code is overloaded in C++ 11. It doesn't conflict with any of other types like int or string, but it takes pointer to char which if used can produce undesired results.

    You can do it like:-

    cout << static_cast<void*>(&b)