From the lectures notes of a course at university, on “call-by-value”:
void fun(int *ip) { *ip =100; }
called by
int n=2; int *np; np = &n; fun(np);would change the value of n to 100.
When we say “int *ip”, what exactly do we mean? A pointer of type integer? If so, when we call fun() with np as its argument, shouldn’t there be an error as np has the address of n, which is not an integer?
And then, 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? I am sure I am missing something. 🙂
No, a pointer to an integer.
nis an integer so there’s no problem.&n,npandipall have the same type in your code:int*.No … we change the value of
*ip, not ofip. That is, we change the value thatippoints to (which is also sometimes called the pointee).