Archived

This topic is now archived and is closed to further replies.

XELON

Where pointers are used?

Recommended Posts

Last term , I learnt C as much as i can.But I think pointers are a little bit complex. As I write a program , where am i going to use a pointer. Is there any place (or situation) that i have to use a pointer. In short words, When i write a program,i never use any pointers. I can do anything with ordinary vaiables.. THANKS

Share this post


Link to post
Share on other sites
Pointers allow dynamic allocation of memory. My favorite example has alwayse been a class called "node" that contains a "node" as one of it''s members. With "regular variables," this would take up an infinite amount of memory, but with pointers, you can initialize each child only when you need it, therefor making it as big or small as you want, throughout the program. Read about linked lists; they are based on this idea.

Share this post


Link to post
Share on other sites
not to mention the archaic usage of far pointers for device access . There is also passing around stuff through functions, but that can be done with references as well.

Share this post


Link to post
Share on other sites


Why would you want to use pointers?

Think about what you do when you design a program. You need to know what sort of data you''ll be working with as well as how much of it there is and how you''ll be accessing it.

The classic example is an array. If you know you''re going to need a max of say 100 elements in your array, you declare it in the code as such. This is a compile-time decision. You''re committing yourself to using 100 elements no matter what.

If you''re not using those 100 elements everytime, but it was a theoretical safe maximum.. then you''re wasting memory on something you might not use 90% of the time when your program is running.

Thus, instead of using a compile-time approach you can use a run-time approach of dynamically allocating memory for your array so that you''re using whatever you need to use ( limited by your hardware ). This requires using pointers.

Other uses for pointers include passing them to functions, which allows you to work on data without copying the data around ( which becomes expensive with large data structures, etc ).

Using the run-time methodology is critical for performance, and good design practice among other things. Pointers make it possible, so you should learn to use them well.


.zfod

Share this post


Link to post
Share on other sites
I''ve just started getting back into C++ and seeing this thread I have something to add. Well more to ask actually...

I''m a little confused on exactly when to pass a pointer to a function or to pass the address. I mean, isn''t it exactly the same thing? Is there a reason why you''d want to do it one way over the other?

eg. Passing the address of an object
void GetPlayerInfo(ifstream &fin, CPlayer &player)

OR

eg. PAssing a pointer to an object
void GetPlayerInfo(ifstream &fin, CPlayer* player)

I going through the text adventure tutorials on GAmeDev and wanted to convert his code to pointers, but then I thought about it and couldn''t really understand why I''d want to do this. In my mind it''s the same thing.

Anyways, if anybody could shed some info on this, I''d appreciate it.

Quiggy.

Share this post


Link to post
Share on other sites