Skip to main content
GameDev.net gamedev.net
🔒 Locked

Newbie: Pointers.... ?!?!?

Started by Estauns Oct 12, 2001 at 9:47 AM 10 replies 500+ views
Original Post
Estauns
Estauns
Hey all, I''m quite the n00b when it comes to C++ and I''m really struggling on Pointers. I just don''t understand them, what are their purpose and what could they be used for? If I have PointerA and VariableB, and the value of VariableB is set to 56, then the value of PointerA is also 56. Now if I change VariableB to 37, PointerA instantly updates to 37. But what the hell is the point of something like that? What practical use do pointers have? Can anyone help? Please?!
"Where genius ends, madness begins."Estauns
Nytegard
Nytegard
This might not help you understand much, but pointers are great for pointer arithmetic. You use pointers when you need to do something that just cannot be done easily or efficiently without them.

Say you have a function that you give some data and you want to return two or more items. Under normal circumstances, unless you use a vector, struct, or something else, it''s just too slow and inefficient.

IE: You write a function to take solve the quadriatice equation.

It''s function prototype might be:

short myquadeq(float a, float b, float c, float *answer1, float *answer2);

You might pass values

short doesitwork = myquaddeq(3.0f, 4.0f ,5.0f, &answer1, &answer2);

Now depending on the value of the returning short, you know if answer1 or answer2 works. If it does, all you have to do is use answer1 or answer2. You passed the address values directly to the other function, so the other function can directly manipulate those values.

Another good use for them could be dynamic arrays.

Lets say you have a function you want to manipulate values of an array, but you don''t know what dimension the array is. It could be array[][] or array[][][][][][]. You can pass the initial pointer to a function, and have pointer arithmetic at work.

Finally, strings are pointers. Each character really has a pointer. You use pointers everywhere. They can make life (at least in programs) much more efficient. I''d suggest taking some time to read up on them and understand them more.
Estauns
Estauns
Okay, I get the whole theory behind it.

Now can someone explain to me the practical use for them? Why can''t I just use the original variable for things like comparrisons and loops or WHATEVER the case might be, why do I have to take up more memory with a pointer just to have the same value as is already defined in a another variable?
"Where genius ends, madness begins."Estauns
siaspete
siaspete
They're good for processing arrays, and passing structures to functions. You don't want to pass large amounts of data between functions, as it can be slow.

Also, if you allocate memory yourself, you will need to use a pointer.

For example

int * pMyIntBuffer = new int [20];

That will allocate an array of 20 ints for you.

delete [] pMyIntBuffer;

That will deallocate it.

Edited by - siaspete on October 12, 2001 3:47:39 PM
felisandria
felisandria
Hehe, yay pointers.

Well the first thing to understand is that a pointer is an address to an actual, physical spot in your RAM (or virtual memory if you''re having to use that, hehe). It''s a place.

It''s sort of similar to your house address. If I tell you my address, you can find my house. If a function is told the address to the information it needs, it can find it. Your address is not your house, it just tells you where it is, like a pointer. Think of a pointer in terms of addressing an envelope (or better yet, addressing an e-mail, as they''re more or less instantaneous).

What are they good for? Well, let''s see.

They''re good for storage. If everything had to be back to back in an array-like structure, then it would be very difficult to make any dynamic structures. Pointers give you the ability to find out where to go next to find the next piece of information, actual-physical-memory-wise, when you reach your first destination.

They''re good for manipulation. You only get one return value for a function, but when you use pointers as the arguments of a function, since pointers hand the function the actual block of memory to play with as they please, a function can change everything it wants. This is also really dangerous, btw.

It''s good for duplication. It lets several people get to your house, in a manner of speaking. Rather than having 300 copies of a struct that takes a big block of memory, I can have 300 pointers to just one copy of a struct that takes a big block of memory, though you have to remember the manipulation danger... if you change the stuff the pointer points to, it changes for all 300.

It can make updating easier, for the danger reason mentioned above. If I have 300 copies of one structure and I want to change a piece of data in it, I have to seek out and change all of them. If I have 300 pointers to one copy of the structure and I need to change the data in it, I change it once and the other 299 pointers at that memory pick it up automatically.

It''s good for hands-on control of when data is allocated and deallocated. With plain variables, you basically get 2 choices: you can make them global or semi-global (class members) and get stuck with a huge chunk of allocated memory all the time, or you can make them local and lose the data all the time. With pointers you can decide when to make a new copy, and when to delete the copy, it''s up to you. (This is sort of part of the dynamic thing.)

Pointers are an incredibly powerful tool. Just keep working with them and eventually you will become comfortable with them.

-fel
~ The opinions stated by this individual are the opinions of this individual and not the opinions of her company, any organization she might be part of, her parrot, or anyone else. ~
amish1234
amish1234
Pointers are obviously shafty. I derived this concept through logical deduction and algeabraic reasoning.

Have you gone on a rampage today?
___________________________________________________________Where to find the intensity (Updated Dec 28, 2004)Member of UBAAG (Unban aftermath Association of Gamedev)
Estauns
Estauns
Hey guys,

Thanks a tonne for the help guys, I really appreciate it. This has really started to put things more into perspective and tightened things up.

Once again, thanks a tonne for the help guys.

Take care of yourselves,
We''ll be in touch,
-Estauns
"Where genius ends, madness begins."Estauns
Zipster
Zipster
Accept pointers as your coding deity and you will advance quickly

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.