Tuesday, May 14, 2013

Learning C++ Pointers for REAL Dummies


Where do you live? (&)

Following along with our smilie, when we declare a variable a person is put into the house so we can access what is stored there.
But, what if we wanted to know where this person lives. This is done by preceding the variable indentifier with an ampersand sign (&), which means "address of." For example:
melissa = &paul;
This line would store the address of paul into melissa's house.
Now, let's suppose paul's address is 1500.
paul = 21;
tom = paul;
melissa = &paul;
So, for this example, the first line will store 21 into paul's house. The second line will store the value 21 inside tom's house. So far no different then what we have usually done. The third line, though, will store the address of paul, 1500, into melissa's house.
The following diagram might help to understand this example:
For a Flash version, click here.
The variable melissa is what we call a pointer. We have not gotten to how we declare a pointer, since it has certain qualities that we will discuss in Don't point, it's rude!

No comments:

Post a Comment