Skip to main content
GameDev.net gamedev.net
🔒 Locked

pointer

Started by theadamSGT Jul 28, 2005 at 7:30 PM 9 replies 1k views
Original Post
theadamSGT
theadamSGT
whats a pointer
picklejuice
picklejuice
it something that "points" to a location in memory, this is usefull for stuff like addressing variables outside their scope (like in a function).
theadamSGT
theadamSGT
i knew that i meant what does it do
picklejuice
picklejuice
it doesn't really do anything, it just contains a memory address, like I said, if you ever need to modify a variable outside the scope of that variable, you can use a pointer to modify it.

from wikipedia:
In computer science, a pointer is a programming language datatype whose value refers directly to ("points to") another value stored elsewhere in the computer memory using its address. Obtaining the value that a pointer refers to is called dereferencing the pointer


theadamSGT
theadamSGT
could you use it in a example
picklejuice
picklejuice
#include <iostream>using namespace std;void f(int* p) { //the function f() takes a pointer to a type int    cout << "p = " << p << endl;  cout << "*p = " << *p << endl; //this is the dereferencing  *p = 5; //this is the part where the pionter is used to modify x  cout << "p = " << p << endl; }int main() {  int x = 47;  cout << "x = " << x << endl;  cout << "&x = " << &x << endl; //this displays the address of variable x  f(&x); //the address to variable x is passed to function f()  /* you should read the above function definition now, and      replace p with "the address of x" and *p with "the value of x"  */  cout << "x = " << x << endl; //the result}


Now f( ) takes a pointer as an argument and dereferences the pointer during assignment, and this causes the outside object x to be modified. The output is:

x = 47
&x = 0065FE00
p = 0065FE00
*p = 47
p = 0065FE00
x = 5

Notice that the value contained in p is the same as the address of x – the pointer p does indeed point to x. If that isn’t convincing enough, when p is dereferenced to assign the value 5, we see that the value of x is now changed to 5 as well.

Thus, passing a pointer into a function will allow that function to modify the outside object.
theadamSGT
theadamSGT
thanks i didnt really understnd it but oh well il understand someday
theadamSGT
theadamSGT
i read the it all i see is assignment
ildave1
ildave1
I would advise you to grab a book. C++ Primer 4th Edition (30 bucks on amazon.com) is what I am reading right now, and It is pretty good. :)
nobodynews
nobodynews
Okay, say that you want to hide some treasure so you make a white grid on your back lawn and label every square 0 through whatever, say 9999. And then you bury the treasure in one of the squares, for instance 1200, and write that location on your paper.

That paper is like the pointer in programming. So, what are they really useful for?

1)getting memory during program run-time that you can't know in advance. For instance, if you load a file you would find the size and then you might load the structure into an array. If it were an image you wouldn't know how large to make the array until you actually load the file.

So what you'd do is tell the compiler "Yo! I want you to set aside some memory and then tell me where it is and I'll write it on this paper so I can find it later, capiche?"

2)Passing around large data structures. Some things are just plain HUGE. Maybe you have a datatype that's several kilobytes large for some reason. And then you decice to pass it into a function which usually COPIES the whole thing. Copying large things takes a ton of time. Imagine if every time you wanted to sit down you had to copy the chair first!

In programming you'd go "Alright function, I got this thing, but it's kinda large and I don't want you to strain yourself. So how about I tell you where to find it and then you can go to it and do whatever it is you do and I'll wait until you get done? Thanks"

3)Abstract datatypes. So far you've probably messed around with arrays a little bit. An array is a collection of data that is all right next to each other... but sometimes that isn't that practical! It's like you're moving and you packed everything into the moving van when suddenly you realize you need to put something RIGHT IN THE MIDDLE. But everything is already packed tight and you gotta move all the stuff around.

In programming a type of datatype known as a linked list overcomes this problem. It's like, instead of putting all your crap in a truck, you give some to each of your friends and they each carry one thing around. And in order to not get lost each person is told to follow the person in front. That's like a linked list for these reasons:

The data in a linked list is like a box you want to move. And the person you are following is like the data in the pointer. It's like I told you "Hey, take this box and follow Joe" and then I might tell Cindy "take THIS box and follow theadamSGT" and I'd give Joe something else and tell him to follow Steve. You are all linked together.

Hope that helped.
C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) ,
theadamSGT
theadamSGT
i think i understand

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.