View Single Post
  #3 (permalink)  
Old 08-02-2007, 06:14 PM
Bench Bench is offline
Full Programmer
Join Date: Jul 2007
Location: UK
Posts: 113
iTrader: (0)
Bench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished road
Welcome to one of the topics in C and C++ which (unnecessarily) causes a lot of confusion to people who first stumble across them.

Firstly, a pointer is a variable which holds an address in memory - Indeed, nothing special about that.. The truth is you should never care where an object is stored... but the data it holds isnt intended to be seen by the user, its intended to be used as a "placeholder" so that objects in memory can be passed around without being copied.

If you like, you can think of pointers in much the same way that Java uses references (Notice how, in java, you never "see" the data contents of a reference, you just know that its there, and at the other end is an object.. Hopefully!)

The main difference is that java is rather good at cleaning up the mess made by its references. On the other hand, C and C++ isn't good at cleaning up mess made by 'pointers', and allows you to do all sorts of horrible things if you're not careful. (I like to think of Java references as "smart pointers", which stop the programmer from doing "bad" things like leaking memory or accessing memory which doesn't belong to the program. Of course, syntax is a little different too, but that's a minor point)

C++ uses pointers for a bunch of things:
- Creating objects at runtime
- Polymorphism (Note that this is done in Java through base-class references. in C++ its done through base-class Pointers)
- As iterators for "raw" arrays (Java uses References as iterators for its collection classes. C/C++ does the same trick with pointers in arrays )
- Also, pointers are used with alot of C-style code, where you'll find yourself dealing directly with low-level constructs (C has no "string" data type, and it uses pointers to deal with arrays of characters.. yuck!)



The kind of example you posted, I believe, is often used as a contrived way of showing how pointers are used for passing references around. The example is completely detached from any real-world use of C++ pointers (as far as I can see anyway, C++ has a better way which avoids pointers). Pointers in C++ don't really become "useful" until you get past the basic language stuff, and onto OO design, data structures, etc.


Personally, I think pointers are the single most badly-explained topic across books, tutorials and reference websites. So, I won't add to that by attempting to give my own example. I think the best way to understand pointers is to leave them alone until you come across a problem where they're actually useful.

__________________

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!

Last edited by Bench : 08-02-2007 at 06:48 PM.
Reply With Quote
The Following User Says Thank You to Bench For This Useful Post:
HelloWorld (08-02-2007)