Some confusion is possible here because both n and np store numbers - however, when compiling, the compiler will use the numbers differently; that is, while
n++;
and
np++;
are both actually arithmetic operations, the generated assembly is different. It is important to remember that, ultimately, all of the data in the computer are numbers. They become different types of data merely because we treat them differently.
Specifically with regards to your example,
*np = 100;
you need to remember that the *
means dereference, and that operation happens before the assignment. It may be clearer with superfluous parenthesis:
(* (np) ) = 100;
or in a different context:
int n = *np;
Now, I must say, it warms my heart when you say,
we change the value of ip to 100, so doesn't that mean that n now has the value that's in the "memory slot" with the address 100?
as it belies what I regard as an important understanding. However, I believe I am right when I say, you must go out of your way to do that kind of thing to pointers:
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.