Jump to content
  • Advertisement


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


Why the hell does anyone need to know pointers?

This topic is 5268 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

What the hell is a pointer for. Finding the address of a variable, I know, but for what damn reason. Can someon: Give me acouple answers as to how a pointer can help my programming. And if I will ever need them for games. ~ from the depths of the ocean

Share this post

Link to post
Share on other sites
You can allocate memory at run time:

int* p = new int[50];

Commonly, you don''t know how much memory you will need in an application, therefore you will need to be able to allocate more memory if needed.

Another use:

struct hog{

void f(hog h);
void f(hog* h);

hog aHog;

f(aHog); /* slow, must copy 5000 doubles */

f(&aHog); /* fast, only must copy a 32-bit int */


Share this post

Link to post
Share on other sites
Pointers also allow you to perform arithmetic on the actual variable's address to a much greater extent than available to automatic and static variables. Also, one pointer variable could be used as an array if enough memory has been allocated.


Edited by - Mathematix on August 11, 2001 6:44:38 PM

Share this post

Link to post
Share on other sites
Yes, you will need pointers to program a game if you use C or C++. If you use soemthing like VB or Java, the pointers are still there but the language wraps them up to "protect" the code.

Share this post

Link to post
Share on other sites
Hmmm... Pointers, eh? The word "Virus" comes to mind immediatly... But seriously, Pointers are one of the things that make C such a powerful language. Besides, even if all your doing is a bunch of printf()/scanf() statements, you still ned a basic knowledge of poiter usage. (thats what the & symbol is there for!)


Vash the Stampede

"Love & Peace!"

Share this post

Link to post
Share on other sites
Guest Anonymous Poster

Pointers are also great to pass to a function that requires a lot of parameters. Er, I mean, you pass pointer to your params, instead of the params themselves.

Here is some code to ''splain:


// here is my parameter structure:

struct ParamStruct
int x;
int x2;
int y;
int y2;
char stuff;
char other;

// now here is a function that uses the above params

int NeatFunction(ParamStruct* ps)
// code that uses params


// to call my function, I''d do this:

ParamStruct ps;

ps.x = 10;
ps.x2 = 20;
ps.y = 30;
ps.y2 = 40;
ps.stuff = 50;
ps.other = 60;

int result = NeatFunction(&ps);

Now instead of passing six params, I just pass one. I use this specifically in a multi-purpose blitter function I wrote, where the majority of the blitter params stay the same from call to call, and only one or two params change at a time. Seems more efficient.

Share this post

Link to post
Share on other sites
Here are some things pointers are good for:

Dynamic memory allocation
int *p = new int[256];

Linked lists ( basically dynamic memory allocation ^-^ )
Binary trees
Changing values in a function ( C++ uses reference parameters for this ).

...But mostly, dynamic memory allocation :-D It''s fun. You''ll love it.


Share this post

Link to post
Share on other sites
Guest Anonymous Poster
people always forget to list the most important reason: pointers create relationships.

Let''s say you have a Monster class/struct. It has an x and y coordinate, hit points, and a current target. For x,y and hp we use ints. What do we use for current target? We use a pointer to whatever it can target. We can''t use a variable and set it equal to whatever we want to target. That would make a copy. By using a pointer our monster "knows about" a target, he doesn''t "have" a target. There are lots of different kinds of relationships and pointers are used for many of them. Some other relationships:

"has": this means that the data is part of a greater whole, the x variable in a point struct is part of the point. We use normal instance variables for the has relationship

"knows about" here we usually use pointers, though I think references would work for static relationships

"owns" this one is also done with pointers, but here there is a responsibilty for creation and destruction

"is a" where one type is a subtype of another, use inheritance

those are the basic ones, when deciding how model something think about what kind of relationship you are making.

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!