🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Little help please...

Started by
5 comments, last by pfresh85 22 years, 10 months ago
I''ve been learning C++ for the past few months (I tried back during school but I gave up because it was too much of a workload). Right now I''m reading C++ for Dummies. Yes it isn''t the best book but it is the one my parents bought me so I live with it. Well I got through the beginning parts pretty well (first 6 or 7 chapters) just because it was stuff I had done before and was used to, stuff like loops and variables and basic stuff like that. I get to the first chapter on pointers. I understand it and it goes fine. Then the next chapter is more about pointers. And now they start doing stuff with them that I don''t understand (passing a pointer to a function and such). I''ve tried rereading it a few times and I still don''t get it. I decided I''d post this up and see if you guys can explain pointers as a whole better to me than the book does. I''ll try rereading the section again while I wait for a response. Thanks.
Advertisement
I''m still a newbie, but I ''kinda'' know pointers. I don''t know how or what to explain but my advice is to read the next few chapters, and you''ll probably have a better understanding once you see what they''re used for.

Well, atleast that''s what I did!


Anonymous Programmer
Anonymous Programmer
Ok I can try reading the next few chapters but this book seems to assume after they "explain" something that you understand it and then the next chapter is usually incorporating the ideas of the old chapter with something from the new chapter. Oh well all I can do is try.
For starters you aren''t really stuck with just that book. I guarantee that virtually any information available in a c++ tutorial book is available online somewhere, it just takes a while searching to find. I''ll give you a quick pointer tutorial here though.

You probably already know this, but what a pointer basically does is specify a location in the computer''s memory where something can be found, whether it''s an int, a float, a struct, or an object of some type.

Some syntax first, in case you''re confused:

data_type *pointer_name;
pointer_name is the name of a pointer to something of type data_type.

&variable_name
is a pointer to the variable variable_name.

*pointer_name
is the actual data that pointer_name, um, points to.

Passing arguments to functions is one good use for pointers. Every time you call a function, the arguments passed to it are copied into an area of memory called the stack. The larger they are, the longer this takes. In a high speed, realtime application like a game these slow downs can waste valuable time. If you make the arguments pointers instead, you will save a lot of time because pointers are generally much smaller than whatever they point to and will therefore take less time to copy. Then you can just use the * operator on the pointers to access the actual data they point to.

Also note that when you pass an argument to a function you''re just passing a copy of it. If you make any changes to the argument variable it won''t affect the original. For example the function:

void foo(int n) { n=5 };

looks to some people like it changes the value of the integer you pass to it. But it doesn''t. If you declare int bar=3 and then call foo(bar), bar will still be 3. The argument is effectively a local variable so any changes you make are only local in scope. If instead you make the function:

void foo(int *n) { *n=5 };

This way the function takes a pointer instead of an integer as an argument, and changes the value at the address of the pointer to 5. So why is this different? A memory address always points to the same place regardless of where it is used, within the function or outside.

This example here is pretty pointless, but it should give you the idea and you can probably see where this could be useful. And keep in mind that this is juse one use of pointers.
I followed you till about the last example then I got a tad confused. I'm going to check out some online C++ tutorials tomorrow and try and learn as much as I can. Tomorrow is my last day of summer so I hope to catch up on some C++ before school starts. Well thanks for the help.

EDIT: Now that I look back on what you said and I'm not deathly tired it makes sense. Ok I think that should help with my pointer dilemna. Now onto the next big thing.

Edited by - pfresh85 on August 7, 2001 11:08:13 AM
Try this.
Pointers specify indeed locations in memory were your object or datatype is stored. you can also use pointers to functions.

This topic is closed to new replies.

Advertisement