(int*) &var 是什么意思? [英] What does (int*) &var mean?
问题描述
(int*) &i
是什么意思?
char i;
int* p = (int*) &i;
...
*p = 1234567892;
...
如果是 * &i
,我会理解.但在这种情况下,这是一个int
".
If it was * &i
, I would understand. But in this case, this an "int
" in there.
推荐答案
&i : 表示取 i 的地址(这是一个字符*)
&i : means to take the address of i (which is a char*)
(int*)&i
:将该指针强制转换为指向整数的指针(这样做不好/错误,但您告诉编译器这样做它甚至不会发出警告)
(int*)&i
: casts that pointer to be a pointer to integer (which is bad/wrong to do, but you told the compiler to do it so it won't even give a warning)
int* p = (int*)&i;
:声明将 i 的指针存储在 p 中(并且也将其强制转换:编译器甚至不会抱怨)
int* p = (int*)&i;
: a statement that says to store the pointer of i in p (and cast it too: the compiler won't even complain)
*p = 1234567892;
:将这个值写入 p 指向的 base 位置的几个字节(尽管 p认为它指向一个int,是char!).其中一个字节将在 i 中结束,但其他字节将覆盖与 i 相邻的字节.
*p = 1234567892;
: write this value, which is several bytes to the base location pointed to by p (which although p thinks it points to an int, is to char!). One of those bytes will end up in i, but the others will over write the bytes neighboring i.
这篇关于(int*) &var 是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!