这里可能会有些混淆,因为 n 和 np 都存储数字 - 但是,在编译时,编译器会以不同的方式使用数字;也就是说,虽然
n++;
和
np++;
两者实际上都是算术运算,生成的程序集不同。重要的是要记住,计算机中的所有数据最终都是数字。它们成为不同类型的数据仅仅是因为我们对待它们的方式不同。
特别是关于你的例子,
*np = 100;
您需要记住*
意味着取消引用,并且该操作发生在分配之前。用多余的括号可能会更清楚:
(* (np) ) = 100;
或在不同的情况下:
int n = *np;
现在,我必须说,当你说,它温暖了我的心,
我们将 ip 的值更改为 100,这是否意味着 n 现在具有地址为 100 的“内存槽”中的值?因为它掩盖了我认为的重要理解。但是,我相信当我说的时候我是对的,你必须不遗余力地对指针做那种事情:
static_cast<int>(np) = 100;
This would do what you described, because it tells the computer to treat the number that is np as a different kind of number; in the same way that static_cast<char*>(np)
would treat the number np points to as a character, rather than as an integer.