Jump to content
  • Advertisement

Archived

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

Kaezin

Pointers...

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

Does anyone know any links to a good site that can explain pointers? Or if anyone have any free time can they explain pointers? I usually understand everything I see in c++ EXCEPT pointers... thanks

Share this post


Link to post
Share on other sites
Advertisement
A pointer is a variable, which hold a pointer to a place in memory.

char some_char; // not a pointer
char *some_char_pointer; // a pointer

some_char_pointer = &some_char;
// some_char_pointer now point to the memory location of some_char

I have no time to explain now, but I will be back ...

Share this post


Link to post
Share on other sites
A pointer is a way of accessing a variable.
For instance:

int a = 5;
cout << a;

This creates a variable named a, and outputs it's value to the screen. Note that you have actually CREATED an int here.

int * b; //A POINTER to an int
b = &a; //& means (memory address of)

This creates a POINTER to an int, and sets it to point to a. Note that you have NOT created an int here: just a pointer to one.

You can now do this:

cout << *b;

This will output the value of the variable pointed to by b (in other words a). The * is necessary when using pointers as otherwise it simply outputs the address of b.

You can also change the value of a through b.

*b = 8;

This sets the value of a to 8. (because b points to a).

I dunno if this is helping, but I'll just plod on anyway...

What you must understand is that when you create a pointer to an int (int * b), you are NOT creating an int. (It's the same with any other data type too) You are simply creating a name by which you can refer to an int. A pointer MUST point to something before it can be used.

int * b;
*b = 5;

This will not work, because b does not point to anything. You may not get an error, but it doesn't work

Now, as for why you might use pointers...

      
void double(int x)
{
//Doubles the value of x

x *= 2;
}

void main()
{
int x = 5;
double(x);
cout << x << endl;
}



You might think that when the value of x is cout'ed, it would be ten, but it' not. It's still 5. This is because when you called double(x), it created a duplicate of x and passed it to double. Any changes made to x inside double were performed on the duplicate. So when you come back to main, x is still 5. We can fix this by using pointers, though. Instead of passing a duplicate of x into double, we are going to pass the MEMORY ADDRESS of x (a pointer). This will allow double to directly change the x in main.

  
void double(int * x) //Now passes an int POINTER instead of value

{
//Doubles the value of x

*x *= 2;
}
void main()
{
int x = 5;
int * y = &x; //Creates a pointer to x

double(y);
cout << x << endl;
}


You should find that x is now 10.

I know that pointers can be confusing, and also that someone trying to explain pointers can be doubly confusing, but I hope this has helped :D

P.S. This took some time to type, and I apologize if someone else has replyed in the meantime.

---------------

I finally got it all together...
...and then forgot where I put it.

Edited by - AdmiralBinary on November 19, 2001 5:02:51 PM

Edited by - AdmiralBinary on November 19, 2001 5:04:03 PM

Share this post


Link to post
Share on other sites
Jeez that was fast... I''m at school ATM, left to go to the bathroom, and when I came back, I already had a couple replies Thanks for the huge explanation too. But I have a question: wouldn''t it be easier (I''m a newbie still) to just have the variable global instead? That way you don''t have to worry about parameters and the like... And also what about pointers to classes? Whats with the -> thing?

Share this post


Link to post
Share on other sites
Im new too, and I somewhat understand Pointers but do not understand why and when they should be used. In that example AdmiralBinary posted why not just do this?

void double(int &x)
{
//Doubles the value of x
x *= 2;
}

void main()
{
int x = 5;
double(x);
cout << x << endl;
}

Share this post


Link to post
Share on other sites
Interesting, thanks for the reply. I was just getting into the dynamic memory stuff and they will probibly save me a headache.

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!