Pointers...

I just started on pointers and I'm quite confused on this bit of code:
1
2
3
4
	int a = 25;
	int* b = &a;

	cout << *b << endl;

I know that it's the same as
1
2
3
4
	int a = 25;
	int b = a;

	cout << b << endl;

but why is that? Is there an actual explanation or is that just the way it is?
Thanks
Last edited on
It's not the same at all. The two are very different.


In the first example... you have one integer (a). You then create a pointer (b) which points to a. When you print *b... the * tells it to "dereference" the pointer, which basically means to go get whatever variable b points to. So by printing *b, you are printing the variable pointed to by b, which in this case is a. So you print a.


In the second example, you create two separate integers (a and b). Then assign b so that it contains the same value as a. Then you print b (instead of printing a as you are in the first example).

EDIT:

try this to see what I mean:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int a = 25;
int* b = &a;

a = 5;  // <- a is now 5

cout << *b << endl;  // <- prints "5"

//   VS

int a = 25;
int b = a;

a = 5;

cout << b << endl;  // <- prints "25" 
Last edited on
Oh, okay I see now, thanks.
Even though you seem to already understand... I made this anyway because I was bored.

http://i50.tinypic.com/fwhpu1.png


Illustrations are fun!
What still sort of confuses me is
cout << *b << endl;
I get the concept, but I don't get the function of the asterisk in front of the 'b'...
It's kind of hard to explain... Urg... pointers are confusing...

EDIT:
In the picture, it says "print what 'b' points to... which is a," but wouldn't
'b' be pointing to '&a' instead of just 'a' because of
int* b = &a;
hopefully a better way of explaining it...?
Last edited on
No, b is &a (that's what the assignment does), and points to just a.
Got it now ;) thanks
If I could add 1 more thing ...

I find it handy to put a leading 'p' on variable names that are pointers - it helps distinguish what is a ptr and what isn't.

1
2
3
4
5
6
7
8
int MyInt = 10;
int *pMyInt = &MyInt;

//also for use with new operator

class CPerson {};

CPerson *pThePerson = new CPerson;


Happy silly season :D Cheers
stab stab stab hungarian notation.

stab stab stab die
stab stab stab hungarian notation.

stab stab stab die

I would have thought that using the 'p' for pointer variables, might make the understanding easier for newbies. Confusion for newbies comes from 'a' being an int, 'b' being a pointer to int, then 'b' is assigned the address of 'a'. It makes sense to those who understand, but I am saying it might be easier if the pointer variables had the leading 'p', and the variable names were words.

HN is not that bad, as long as one doesn't get carried away with it. I have seen lots of code which uses p for pointers. As for the classes, Qt uses Q, and KDE uses K.

Where HN is bad, is when it is the meaning isn't clear. If it is restricted to just pointers & classes say - then I don't see it as being a problem. The meaning of the notation should be obvious at declaration, but the main responsibility for meaning still lies with the name of the variable.
Topic archived. No new replies allowed.