pointers explained ?

Started by
11 comments, last by obi-wan shinobi 18 years, 10 months ago
I have been reading a few book on c++ ... // N00b-talk I have trouble understanding pointers and their use in programming. I have been through the books "Sams Teach Yourself C++ in 24 Hours" & "Beginning C++ Game Programming" It is explained there, but I still don't get the real use of pointers. Can somebody explain the use of pointers and why you should use a pointer instead of a normal variable ? In short, please! I know there can be a very long answer, but I don't feel I am that far from understanding the use of them, just need a puff in the right direction.
Advertisement
Quote:Original post by kipran
"Sams Teach Yourself C++ in 24 Hours"


I've been using that book too, I thought it was pretty clear on pointers... after I'd read it about 5 times.

If you have another look at Jesse Liberty's code, and read the Analysis underneath (ignoring the stupid Animal noises he seems to use for illustrating things), it'll all fall into place.

But this is a better and easier tutorial. In fact, all of the ones on that site are pretty damn good. I've been programming for some time, and some of the stuff in that Sams book is still a little bit over my head. That link is very good.

They can be used to modify an argument inside a function (there are many other uses, but this one is pretty common):
void func(int* i) {    (*i)++;}// ... in main ...int i = 0;func(&i);std::cout << i << std::endl;  // Prints 1
Although, for this I prefer references (they are the same thing as pointers, except the syntax is easier):
void func(int& i) {    i++;}// ... in main ...int i = 0;func(i);std::cout << i << std::endl;  // Prints 1

Arrays are also pointers, so you could say that that's another use.
A pointer is, as it's name suggests, a pointer to a variable. It contains the address of that variable's value in system memory. More importantly, allocating memory on the heap (RAM) requires the use of pointers. When you call new, the function returns to you a pointer to the memory you just allocated for your new variable.

You can pass that pointer around, modify it, copy it, cast it, and do a number of operations to it just like you could with another data type. Except, because the pointer is usually just a disguised unsigned int (as far as the CPU is concerned) moving/copying it becomes a whole lot more efficient thant copying or moving entire structs/classes around.

And then of course, there are arrays, which are really just pointers to blocks of contiugous memory.

I dunno if I can explain it any better than any of the books you've read. Here's a video made by some people at stanford about pointers. It's a bit kooky/lame, but good for beginners.
The best pointer explanation I have -ever- seen is right here:

http://pweb.netcom.com/~tjensen/ptr/pointers.htm

Read this and you will learn a lot.
A pointer is an address, pointing to a location in memory. You can use it to get to a variable without knowing where it came from.

Example:

int variable;int *pointer;pointer = &variable; // Pointer now holds the address of variable.*pointer = 42; // Dereference the pointer, and assign a value. This sets variable to 42.


Okay, the above is pretty useless, and is just making things harder. Pointers are slightly more useful when passed to functions:

void do_something(int *pointer) {  *pointer += 42; }// ...int a = 5, b = 54;do_something(&a);do_something(&b);


The above will increase a and b by 42.

Related to pointers are references. These are like pointers, but can't be null, and you don't need to dereference them, you can use them like normal variables, and you can't change what they're pointing to. Same as above, but using references:


void do_something(int &reference) {  reference += 42; }// ...int a = 5, b = 54;do_something(a);do_something(b);
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
I have found that this is the best analogy about pointers:

Pointers are to data, what HTML links are to actual web pages. Thus, they're used in largely the same way. If you want to show your friend a web site [a function some data], do you copy the entirety of the site and send that to them? No, you just send a link [give the function a pointer]. It's smaller, it's faster, it's easier. Do you save the entirety of sites, or just bookmark the links? Why?

With links [pointers], if your friend looks at the site [data] and does stuff to it, it actually effects the site [data] not your copy of the site [copy of the data]. And the friend can keep the link forever, and always see the site when it's updated [the data when it's changed]. If the friend has a copy, they only see the copy, not the actual site [data].

Similarly, they have the same downfalls. If you have a link [pointer] and the webserver goes down [the data gets deleted] or doesn't exist [the pointer wasn't assigned to data], the link won't work [your program will do bad stuff].
hmm, that HTML analogy isn't bad, actually. :P

Anyway, I'd suggest this:
- Read what you can on pointers, make sure to do the exercises in the book, so that you know how pointers work, even if you can't see the point.
- Then forget about it. Move on, write programs any way you like. Sooner or later, you'll get to a point where you think "Oh, a pointer would be useful here", and voila, suddenly you'll understand how they're useful. ;)

Pointers take some getting used to, and when you're just getting started, it can be hard to see the point. So don't try to see the point, just make sure you know about them, so you can recognize situations that require them later on.
wow!
Thank you for all the excellent answers, I haven't had time to go through all your replies, but I will later tonight, and I am sure I will get the use of pointers explained thoroughly.
That video wasn't half bad :P Cheesy? Very. Useful? Yes.

That website with all the material on chaptersa is excellent!!! I have to take a closer look at that.

The HTML analogy was pretty good too ;)

This topic is closed to new replies.

Advertisement