View Single Post
  #5 (permalink)  
Old 08-02-2007, 08:38 PM
rpgfan3233 rpgfan3233 is offline
PT Staff
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 4
Join Date: Jul 2007
Posts: 118
iTrader: (0)
rpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura about
Quote:
Originally Posted by HelloWorld View Post
Thanx a lot Bench
Let me know if I'm wrong, so pointer is basically change the primitive type variable to be a reference type? Is that what's pointer does? (Since that's what a reference type variable a.k.a Object does too... )
Not quite. Really a pointer is just a pointer to a memory address. You could do the following in C++ and get a hexadecimal address:
Code:
using std::cout;
using std::endl;

int x = 4;
int* x_ptr = &x;
cout << x_ptr << ":\t" << *x_ptr << endl; //hexadecimal address followed by value held at that address
(*x_ptr)++; //increment the value at x_ptr
cout << &x << ":\t" << x << endl; //same idea, but after the increment of the value at the address held by the pointer and using the actual variable, not the pointer to the variable
A pointer is really just a special term for a variable that holds a memory address. If you modify the value at that memory address, it means that the value of the pointee (the address being pointed to) is also changed. By the way, C++ has references too, though I'm not sure they are anything like Java's references:
Code:
using std::cout;
using std::endl;

int x = 4;
int& x_ref = x;
cout << &x_ref << ":\t" << x_ref << endl; //hexadecimal address followed by value held at that address
x_ref++; //increment the value at x_ref
cout << &x << ":\t" << x << endl; //same idea, but after the increment of the value of the reference and using the actual variable, not the reference to the variable
Strange, huh? It seems like the reference is a copy of the original variable, right? Think about this though: why are the addresses the same? A reference is similar to a pointer, but there is one really big difference between them: a reference only references a single variable for the duration of the scope of the reference. A pointer can be reassigned many times to various memory addresses if necessary. It is essentially the object itself. A good explanation of what a reference is can be found at [8] References, C++ FAQ Lite

The simplest explanation of reference vs pointer:
pointers hold a memory address, whether it is the address of a variable or a NULL pointer, where there is no memory address held
references are aliases to the object it represents

Arrays are really just pointers since an array is merely a start address (wherever the start of the data is) and an memory offset (value) for each element that the array holds. *(x + n) is the equivalent of x[n]. If you really wanted to, you could get the offset address (not to be confused the actual memory offset value) using (x + n) instead of &x[n]. To get the memory offset (value), you would use (x + n) - x or &x[n] - x. As you can see, things can get a bit complicated when working with pointers, which is why a programmer must be careful when using them. References are only as safe as how you use them, so while slightly "safer" than pointers, don't think of them as a cushion to fall back on because that can lead to error-prone code.

__________________
"C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off."
-- Bjarne Stroustrup, creator of what is now known as C++
For more quotes by Bjarne Stroustrup, check out http://www.research.att.com/~bs/bs_faq.html#really-say-that.
Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
The Following User Says Thank You to rpgfan3233 For This Useful Post:
HelloWorld (08-03-2007)