Understanding Pointers

Started by
5 comments, last by y2rr 22 years, 5 months ago
I am a newbie to C++. I know what pointers are and how to use them, but I was wondering in what sort of programs are they useful in.
Advertisement
Well it really depends, but they can be useful in a lot of programs.

I believe Pointers are mostly used dynamic data (such as arrays, link-lists, and trees) and for parameters in functions.

I would post an example of a link-list, but unfortunately, I don''t have the time at the moment. I''ll let someone below explain them if they like, or you can search the net for "Link List C++", and may get some hits.

As for uses, A game I''m trying to work on uses Link Lists for the bullets flying up the screen (over head scroller). Each bullet is an element in the list, and gets deleted when it moves off the screen. That may be abit overkill, i''m not sure. I''ve never made a game before, so this is my first run through.

Also, something you should keep in mind, Arrays are basically pointers. Again, it''s a little more in depth than that, but i must go.

Hope that helps
- NW
quote:Original post by y2rr
I am a newbie to C++.
I know what pointers are and how to use them, but I was wondering in what sort of programs are they useful in.


one good thing is that you can pass a reference to a variable to a function. Then the function can remotely change that variable.

So, you know the problem of only being able to return one variable from a function? Well pointer solve that.

and you can dynamically allocate memory which is good for when you don''t know how much memory you will need.

A CRPG in development...

Need help? Well, go FAQ yourself.
Need help? Well, go FAQ yourself. "Just don't look at the hole." -- Unspoken_Magi
quote:
I know what pointers are and how to use them...

Excellent young Jedi, you have made a leep many fail to land.
I still remember the day I understood what & did and what a pointer really was - suddenly scanf made much more sense.

quote:
...I was wondering in what sort of programs are they useful in.

Ummm, all of them!
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
quote:Original post by Magmai Kai Holmlor
-quote---
...I was wondering in what sort of programs are they useful in.
-/quote---
Ummm, all of them!

Very true!

A pointer, as you know, points to a memory location. Thus, you can manipulate the contents of that location by dereferencing the pointer, or manipulate another memory location by modifying the pointer (changing where it points). This is useful with arrays because you can address an array simply by its first element; to access the element, move the pointer one space forward (and since all memory addresses on 32-bit machines are of size 4 - 4 bytes - this works for all types of "containers.")

Understand where, when and how to use pointers is critical to writing efficient C/C++ code. There are so many instances in which pointers are used that the best advice I can give you is to find some source code (that does something moderately complex, but is still comprehensible to you) and note all the places where it uses pointers. Try to figure out why and how the pointers are used in that program, and think about what you''d have to do to implement the program without using pointers.

Good luck, and may the Source be with you.


I wanna work for Microsoft!
It''s also great when passing a lot of data to a function in a game ... When you pass, for example, a structure to a function the whole structure has to be copied, when you pass a pointer to the structure only the pointer is copied ... great speed increase if you oftenly use the function ... And as someone else said, when passing a pointer the function can actually munipulate that structure without having to return it ( saves you another mem copy ) ...
Or when you have a load of tiles that have to draw themselves through a rendering system class ... you can have all the tiles own a pointer to the class ... without pointers this would mean every tile has it''s own rendering class ( not a good idear )

Pointer just have so many uses it''s inpossible to explain them all, just try to get a good grip of them, and when you''re a good programmer their uses will come to you while programming
Stefan
If you are on this forum it''s because you want to program games right?
Well, I hope you won''t be surprised to learn that the graphics APIs used in game programming like DirectX or SDL rely 100% on pointers.
So no pointers, no games.


This topic is closed to new replies.

Advertisement