Assignment of address to integer variable

前端 未结 4 987
萌比男神i
萌比男神i 2021-01-19 00:43

How come you can assign an address to an integer variable like this,the complier will not give an error. i always though you can only assign integer values to a integer vari

4条回答
  •  有刺的猬
    2021-01-19 01:24

    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.

提交回复
热议问题