Assignment of address to integer variable

自古美人都是妖i 提交于 2019-12-01 17:59:00

The number 0x28ff1c is just the hexadecimal (base-16) representation of the decimal (base-10) number 2686748. As cout defaults to printing decimal values for integers, that is probably the number you got printed.

The case with char b = 0x28ff1c is slightly different, because

  1. char is not large enough to hold that value. The practical result is that it gets truncated to 0x1c.
  2. cout treats char specially, because it is normally used to hold textual data, so cout prints the character that has the code 0x1c, which is some kind of control character. You could try it with 0x41 for example (which represents 'A' in ASCII and UTF-8).

And note that there is nothing that marks 0x28ff1c as being an address. An address would be formed by &a or (void*)0x28ff1c.

0x28ff1c is not an address itself - it's just a hexadecimal number.

The following are equivalent:

int a =   2686748;  //decimal number
int a =  0x28ff1c;  //hexadecimal number
int a = 012177434;  //octal number

An address is represented by a pointer - if it's just that, an address, you can use a void*:

void* p = (void*)0x28ff1c;

In which case

int a = p;

wouldn't compile. p is an address, the number itself isn't.

Because in any literal starting 0x is actually an integer. So it is allowed. An address is can sometimes be an integer.

You can also use *(int *)(Address) = value as a construct to assigne a value to Address and use the answer by @Luchian Grigore

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