地址分配给整数变量 [英] Assignment of address to integer variable
问题描述
如何以这样的方式为整数变量分配地址,编译器不会给出错误。我总是尽管只能为整数变量赋值整数值。
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 variable
int a=0x28ff1c
您也可以对char变量执行相同操作,编译器不会给出错误
You can do the same for a char variable, the complier will not give a error
char b=0x28ff1c
char b的屏幕垃圾值和int a的随机值
It will output on the console screen rubbish value for char b and a random value for int a
cout<<b
<<endl;
cout<<a;
有人可以向我解释为什么char b和int a的输出有差异。
有人可以向我解释为什么char变量和整数变量可以有地址分配
Can someone explain to me why there is a difference in the output for char b and int a. Can someone aslo explain to me why a char variable and integer variable can have addresses assign to it
推荐答案
0x28ff1c
只是十进制(base-10)数字 2686748
的十六进制(base-16)表示形式。由于 cout
默认为整数打印十进制值,这可能是您打印的数字。
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.
char b = 0x28ff1c
稍有不同,因为
-
char
不够大,不能容纳该值。实际结果是它被截断为0x1c
。 -
cout
char
特别地,因为它通常用于保存文本数据,因此cout
输出代码0x1c
,这是某种控制字符。你可以尝试用0x41
例如(它代表ASCII和UTF-8中的'A'
)。 li>
char
is not large enough to hold that value. The practical result is that it gets truncated to0x1c
.cout
treatschar
specially, because it is normally used to hold textual data, socout
prints the character that has the code0x1c
, which is some kind of control character. You could try it with0x41
for example (which represents'A'
in ASCII and UTF-8).
注意,没有任何标记 0x28ff1c
作为地址。地址将由& a
或(void *)0x28ff1c
形成。
And note that there is nothing that marks 0x28ff1c
as being an address. An address would be formed by &a
or (void*)0x28ff1c
.
这篇关于地址分配给整数变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!