Sign in to follow this  
kipran

pointers explained ?

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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);


Share this post


Link to post
Share on other sites
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].

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 ;)

Share this post


Link to post
Share on other sites
That Binky video was a decent beginner lesson on pointers, even though the most enjoyable part for me was when he got zapped into pieces (lol).

Anyway, one use of pointers is to dynamically allocate space for an array:


void main() {

int num = 1;
cout << "How many elements: ";
cin >> num;
if(num < 1) {
cout << "It must be above 0!";
return; //end program immediately
}
int array[num]; //causes about 3 errors in C++
int *array = new int[num]; //works - an array size determined at runtime
if(array==NULL) {
cout << "Memory unavailable!";
return;
}
for(int i=0;i<num;i++) //for every element in the array...
cin >> array[i]; //...assign it the value you want it to have
delete array; //frees memory, only call once per pointer
delete array; //this second call may crash your program
}

Share this post


Link to post
Share on other sites
Quote:
Original post by obi-wan shinobi

Anyway, one use of pointers is to dynamically allocate space for an array:



Only if you're feeling particularly masochistic, or using plain C, yet I repeat myself.

[appolgies to Mark Twain.]

Share this post


Link to post
Share on other sites
Quote:
Original post by Telastyn
Quote:
Original post by obi-wan shinobi

Anyway, one use of pointers is to dynamically allocate space for an array:



Only if you're feeling particularly masochistic, or using plain C, yet I repeat myself.

[appolgies to Mark Twain.]


At the very least, it'll have to do until I master vectors... :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this