Skip to main content
gamedev.net
🔒 Locked

*pointer or &ref -- whats the diff

Started by MTclip Jul 31, 2005 at 1:51 PM 9 replies 800+ views
Original Post
MTclip
MTclip
tell me do i have this concept correct.. a pointer ( int * pInt ) holds the address of a variable.. a refrence ( int & rInt ) holds the address of a variable.. so they are the same? if not- maybe a small example of when to use each?
b34r
b34r
Yep, but a reference cannot be NULL (unless you do some ugly things like ptr=NULL; func(*ptr);).
Praise the alternative.
Oluseyi
Oluseyi
They're not the same, and it's a "deficiency" of C++.

Fundamentally, pointers and references encapsulate the same quantity: the address of a variable. Practically, however, a pointer exposes that address to manipulation by the programmer while a reference only exposes the value pointed to. You can think of a reference as a compiler-managed pointer, meaning that you should use it unless you specifically need access to the address itself.
pinacolada
pinacolada
What Oluseyi just said using big words is: you can't make a reference point to something else. That's the other big difference.

There are a lot of situations where you could go either way. The basic style guideline is: use references whenever you can, use pointers when you need to.
MTclip
MTclip
okay so say i have 2 pointers to some object
and i want to compare the addresses of the object from the pointers...
int pooey = 88;int* num1 = pooey;int* num2 = pooey;//now i want to compare the//address of what is pointed to if(num1 == num2)if(*num1 == *num2)if(&num1 == &num2)if(*&num1 == *&num2)//mmm.. which should i use//or should i int* num1 = &pooeyint* num2 = &pooey


thanks

---------------------- ;P
pinacolada
pinacolada

int* num1 = &pooeyint* num2 = &pooeyif(num1 == num2)


will check if they are pointing at the same thing. Now if you wanted to compare the values they are pointing to, that would be different code.

Also, when I was first learning C, the different usages of the * and & symbols confused me for a while. Just remember: * and & mean very different things when they are used on a value versus when they are used on a type.
zoggo
zoggo
int pooey = 88;      // Fine// Both lines, same problem:// num1 is a pointer which stores and address. The = operator sets the address.// If you want the address to be 88, this is find but will need a cast. If you// want the address to be that of pooey, you want to change it to num1 = &pooeyint* num1 = pooey;int* num2 = pooey;// Compares addresses. Fine but may not be what you wan't.if(num1 == num2)// Compares values. Fine.if(*num1 == *num2)// Compares addresses, but the addresses compared are that of num1 and num2,// not the addresses STORED by num1 and num2. Again, legal, but probably// not what you want.if(&num1 == &num2)// For ints, this is the same as the first if statement. Compares addresses.if(*&num1 == *&num2)

No references here at all. I was going to write whether each statement evaluated to true or false but since your initial assingments are a bit odd, it probably wont help much.
Deyja
Deyja
References also behave polymorphically. A reference to a base class could very well actually refer to a derived class, and will behave just as it is was a pointer. All virtual functions will be dispatched to the proper dynamic type of the reference.
dyerseve
dyerseve
if(*num1 == *num2)

That is probably going to crash your program since chances are 88 doesnt point to a valid page in memory.

You probably want:
int* num1=&pooey

The & operator gives you the address of something. A pointer holds that address.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.