Jump to content
  • Advertisement
Sign in to follow this  
bikola_p

pointers??

This topic is 4835 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

Hey guys, i never paid attention to pointers and always avoided reading on them. What is the point of a pointer. Cant many variable names make up for them? Whats a real life case where a pointer is used.

Share this post


Link to post
Share on other sites
Advertisement
What's the point of using pronouns in English?

You can have the same sentence (code) be applicable to different persons (variables) in different circumstances. Without them, you would have to re-write your sentence (code) with each person's name every time.



Pointers let you refer to another variable without naming that variable (or even a function) explicitely. By changing the value of the pointer, you can have your code "automatically" start working with another variable, with no other changes required. Additionally, when you dynamically allocate memory to create new variables at run time (e.g. your ship is firing many laser beams and you need a variable to store each laser beam), what you get is a pointer - the variables didn't exist when you wrote and compiled your program, so they don't have a 'name'.


To computer, a variable's real "name" is its address in memory. Pointers are variable which are used to store such addresses.

Share this post


Link to post
Share on other sites
One of the main uses for pointers is to gain efficiency. Say you've got a hundred space invaders on screen (what a game that'd be!), and each uses the same images for its animation. Now, each space invader object could have its own copy of these images, but that would be a horrible waste of memory; if each space invader were, in fact, to just have a pointer to one set of the images in memory, then things are suddenly a lot better.

Yes, I know that there would be other ways to get around this, and the example is a bit of a bad one, but it might aid in getting the idea across.

On a similar note, whenever you pass a variable (or object) into a function, a copy of that parameter must be made to the function's local stack. Now, copying a float or something isn't that big of a deal, but copying, say, the vertices of your player's character model would be very inefficient. So for any function that acts on the character's model, or any other large amount of data in memory, you would want to pass a pointer rather than the vertices themselves.

So yes, pointers are mainly used for efficiency. Hope that helps.

Share this post


Link to post
Share on other sites
thanks i get what you mean, just studying as i have to use linked list to write a video store database app for uni.

Share this post


Link to post
Share on other sites
try reading the sams teach urslef c++ in 24 hrs in regards to the pointer and reference sections, i feel (although im not a big fan of these books) that they do a good job of giving you an overview and some good clear examples.

What is a pointer, basically its a variable that stores a memory address.

so the pointer ( int * pointer ) is a pointer to an integer

pointer is the name of the pointer and we know its a pointer as we have the '*' before it.

so to 'declare' a pointer you give a type that it points to, int, char, float use the * after the type declaration and then give it a name. Normally pointer variables start with a p, so if you wanted to point to an int variable you would create a pointer like

int * pPointertoanint (although you wouldnt name pointer varibales this way as it is confusing)

so, whats the use of this, mainly its to do with memory, as i said at the start we point a pointer is a variable that points to a memory address, so doing this

void main()
{
int age = 10;
int * pAge;

pAge = &age;

cout << pAge << endl;
cout << *pAge << endl;
}


what we have done here is
1. Create a regular int variabe called age and assign a value 10 to it

2. Create a pointer to an int called pAge (this does not automatically point to age just because its called that)

3. we then assign the pointer pAge to the address of age, notice to do that we use the & symbol, if you dont use the & symbol u get an error saying that the pointer variable isnt pointing to a memory address, after all thats all a pointer does

4. print the pointer variable to the screen . . if this is done you will see a hex value, this is the memory address of the int variable age.

5. If you dereference the pointer by simply using the * symbol befoire the pointer name, you can print out the value stored at the address to which the pointer points . . .

At first this seems daft but the more you work with pointers and get used to the way they look and how they are used it becomes second nature.

the most consfusing aspects of this is the declaration and dereference parts, int * a, delares a pointer where *a on its own is a value, also later you seee that the & symbol is used as a reference, its all code, its all crazy and its hard, anyway, im going to go for my dinner, dont read too far into what ive written above as im a learner in this field myself

Share this post


Link to post
Share on other sites
Quote:
Original post by bikola_p
Hey guys, i never paid attention to pointers and always avoided reading on them.

Maybe it's time to read on them then? [wink]
If you can't be bothered to even do that, then I doubt anything people say here is going to help you.

Share this post


Link to post
Share on other sites
I like to think of them as house addresses. Back in caveman days, none of the caves needed addresses because there were only like 3 caves, and if you wanted to talk about a certain cave, you could just grunt in its general direction. Similarly, in small projects you don't really need pointers, because you only have a couple pieces of data to work with. But now that our society is huge, we need to have addresses and zip codes and stuff. It would be impossible to order pizza delivery without addresses. And similarly, when your C project gets bigger, pointers become really useful, and a lot of things would be impossible without them.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!