The address of character type variable

不想你离开。 提交于 2019-12-01 21:27:45

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)

There is an overload for << which takes a pointer to char and interprets it as a terminated C-style string. Using this for the address of any other char will go horribly wrong.

Instead, convert to a typeless pointer so that << doesn't get too clever:

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

Expression &b has type char *. When operator << used fo an object of type char * it considers it as a string and outputs it as a string. To output the address you should write

( void * ) &b

or

reinterpret_cast<void *>( &b )
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!