I need help with pointers

Started by
12 comments, last by asdasd12345 20 years, 6 months ago
So far Ive been reading my C++ book and looking up tutorials online and I really dont understand the point of pointers. I mean what kind of situations would you really want to use them in. As far as I understand them pointers are variables that hold the address of another variable..or something like that. Any help would be helpful, apart from help that isnt helpful.
http://www.geocities.com/asdasd12345/
Advertisement
Pointers are useful when you don''t know the size of something.. like strings. Strings can be any length.

You might use them for reading file data.. ie

char *data;

//Get filesize somehow

data = new char[filesize];

//read filedata into data


Someone else can add on to that

The main reason of using pointers is the following:

void AddFunction(int a, int b, int result){  result = a + b;  std::cout << "inside: " << z << std::endl;}main(){  int x,y,z;  x=5;  y=4;  z=1;  AddFunction(x,y,z);  std::cout << "outside: " << z << std::endl;}

This will output:
inside: 9
outside: 1

This happens because if you do not pass a pointer to z, the computer makes a local COPY of variable z inside the function. This local copy is overwritten by the calculation. After the function finishes, this local copy is destroyed, leaving the original z unaltered.

void AddFunction(int a, int b, int* result){  (*result) = a + b;  std::cout << "inside: " << z << std::endl;}main(){  int x,y,z;  x=5;  y=4;  z=1;  AddFunction(x,y,&z);  std::cout << "outside: " << z << std::endl;}

This will output:
inside: 9
outside: 9

Because you passed a pointer to z, the original value was changed, and this change persists after the function is finished.

A second reason for using pointers then also becomes clear. Image if you are not passing just an int, but some kind of very large custom class. This will take time to copy. Using a pointer will make it so the computer does not have to copy anything but the addresse of the object, and this addresse is small.

ps. Yes I know the AddFunction() is not very useful in this way, but it was the only example I could come up with.
quote:Original post by asdasd12345
So far Ive been reading my C++ book and looking up tutorials online and I really dont understand the point of pointers. I mean what kind of situations would you really want to use them in. As far as I understand them pointers are variables that hold the address of another variable..or something like that. Any help would be helpful, apart from help that isnt helpful.


The worst part of most pointer lessons just involve pointers to int and stuff. That''s useful in some cases but really doesn''t help beginners understand why to use them.

Not sure if you know much about structs or classes but in a gaming situation if you have a Fighter struct or class it probably will store all sorts of info... name, hit points, strength, experience points, turn-ons... Lots of things basically. The size of a Fighter (all the info it stores) could be big, lets say its maybe 100 bytes total in this case.

Pointers are most useful when you want do something in a function with a big object. So then, in your game, you want your Fighter to do stuff like get stronger and have more hit points. So you have a function that calculates how much experience he gains after a fight. Since the calculations could be based on other current stats you have two basic options... pass by value or pass by pointer.

If you pass by value, you have your obese 100 byte Fighter make a 100 byte copy of itself for your function to work with and return that copy to whoever called it.

If you pass as a pointer, you are just telling your program that oh, my Fighter is over here (in memory), if you want to give him a gun, he''s right there, go hand it to him. Instead of passing 100 bytes, you pass 4 bytes (the size of a pointer) and you can mess with your Fighter''s stats directly. That''s 25 times less information your program has to shuffle around when you want to do a simple calculation with something big.

There are other uses but stuff like the example above hopefully explains the power of using pointers in actually programming. Ask more questions if this didn''t make any sense.

Pointers have many uses. For instance you will often want to link classes (keeping in mind that you would usually have a class in it''s own seperate file) to eachother and the only way is through pointers. But if you are just learning, it is unlikely that you will understand that just yet. So here is simpler example of the use of pointers:

struct Monster
{
int x;
int y;
Monster* target;
}

Here is a representation of a monster, which you would then program to attack other monsters - target being the pointer that holds another monster that the monster is attacking.
And, this being one of the greatest uses of pointers known to mankind:
LINKED LISTS
Hi man, you sound exactly like me a couple of months ago. To understand pointers you need to understand memory and how your variables are stored in it. The best explanation i''ve come across on the net is at http://cslibrary.stanford.edu/
Its in c but there is a 30 page pdf explaining memory and pointers, a short lesson using c, c++ and java source to explain pointers and a short animation that makes pointers quite clear. It all made sense after i found that.
Good luck
--------------------------------------------------------------------- There are 10 types of people in this world, those that understand binary and those who don't.
I've found the best way to learn things is to learn their applications. So try making some linked lists, and implement them in a practical program. Pointers are also useful for C-style strings. Dynamic arrays are allocated using pointers. All kinds of things, really...

my best advice is just to keep coding and coding. As your program becomes increasingly sophisticated, the methods you use to code it will too. Practice makes perfect - if you can't figure out a reason for using pointers in your current project then that's fine. Don't try to come to pointers, make them come to you - you will need them eventually, and when you do, you will know it.

edit: don't any of you know how to pass variables as reference parameters? Well I'm sure you do but i just haven't seen it mentioned... Pretty much eliminates the need for passing your big object into the function as a pointer.

[edited by - Odoacer on October 6, 2003 12:18:42 AM]
I use pointers mainly for dynamic memory, but thats a whole nother ball park I suppose.
Here''s an other example of pointer use. While there''s an alternative to pretty much all above examples (though quite a painful one, often!), I can''t think of any way to accomplish this without a pointer.

Writing directly into memory.

There are many, many applications of the above. Suppose you want an ultra-fast drawing routine, you can''t just rely on the GDI''s SetPixel() or SetPixelV() functions. You''d have to get down and dirty and write directly into memory, which is only possible either via a function provided by an API (see little note on how SetPixel just won''t do the trick for our example above) or via a pointer to the video ram. You can''t store the whole video ram into an array because the changes won''t be reflected anywhere: you need to tell your program WHERE to write stuff as well as WHAT to change. Hence the need for a pointer.
This is well known: "Any software engineering problem can be solved by adding a level of indirection."

Pointers allows to add levels of indirection.

Concluson: Any software engineering problem can be solved by using pointers.

[ Games made by gamers'' minds | maxiInnovation - miniCan | mail: ChristianRoesch@gmx.de ]
[ Games made by gamers' minds | maxiInnovation - miniCan | mail: ChristianRoesch@gmx.de ]

This topic is closed to new replies.

Advertisement