views:

170

answers:

8
+1  A: 

Yes, when you use int * as a parameter type of fun you are saying it accepts a pointer to an int. Since np is a pointer to an int passing it into fun is ok.

np and ip are pointing to the address of n, so when you assign a value like *ip = 42, it will assign the value of 42 to the memory location that ip is pointing to - which is n.

Trent
So "int *pointer" means "a pointer to an integer". But of what type is that variable/element? Because to me *pointer (which is a value) and np (which is an address) are not the same thing.
sebkom
In a declaration of a pointer you use `int*` to indicate that you want a pointer to an int. When later using the pointer as a variable, you add a `*`, like in `*ip`, to access the value the pointer is pointing to.
ahans
+8  A: 

A pointer of type integer?

No, a pointer to an integer.

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?

n is an integer so there’s no problem. &n, np and ip all have the same type in your code: int*.

And then, we change the value of ip to 100

No … we change the value of *ip, not of ip. That is, we change the value that ip points to (which is also sometimes called the pointee).

Konrad Rudolph
One thing that might help clarifying here is to stress that the type is `int*`, even though it's written `int *ip` - C++ allows us to write it either way (`int* ip` or `int *ip`) but both mean the same thing.
Amber
Yeah, but there's a gotcha: If you say `int* ip, ip2;`, `ip` is a pointer to `int`, but `ip2` is just an `int`. The `*` associates with the name, not the type. Thus people write `int *ip;` to remind themselves of this.
Mike DeSimone
@jamesdlin: I'm aware of this - the comment I made was not meaning to imply that it was specific to C++; I merely used C++ because that is what the OP asked about. :)
Amber
A: 

What you have in the sample code is called pass by pointer. Your function has a parameter of type pointer to integer. So the value passed to the function is the address of the variable n. The function assigns 100 to the variable (i.e. a memory location) pointed to by the pointer parameter ip. The star * is the dereference operator in C/C++.

Nikolai N Fetissov
+1  A: 

type* used as a type means "a pointer to that type"

common use is int *var, but I like to write it as int* var as that makes it a better distinction from dereferenceing a pointer, i.e. *ptr.

so int* a means, a is a pointer to an object of type int, *a means the object pointed to by a and &a means the address of object a.

wich
+1  A: 

in fun(), you aren't changing the value of ip to 100, you're changing the at the memory location pointed to by ip.

Brian Schantz
A: 

think of np as of type int*. Bear in mind that for this purpose the asterisk and the int together make a "pointer to int" type, and are not separatable.

alemjerus
+1  A: 

ip is a pointer, with ip* you access the "memory slot" it's pointing to. np is also a pointer. Using &n you assign the address of the memory slot of n to np. So when calling fun(), inside fun() the only memory slot that's accessed is the one of n and thus to n 100 is assigned.

ahans
A: 

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.

Narfanator