Skip to main content
gamedev.net
🔒 Locked

Quick Pointer Question

Started by subnet_rx 7 replies 250+ views
Roof Top Pew Wee
Roof Top Pew Wee
The * is dereferencing the pointer. What pointers are is just variables that store an address. So for example:

xPtr = &x

That gets the address of x and stores it in xPtr. If you were to do:

cout << xPtr;

You''d get some address like 0x0000001 or something like that. If you type *xPtr, instead of getting the address, you get the value of whatever variable is held at the address it holds. So in this case *xPtr would just give you the value of x. Just a note, this is a pretty basic question and I''d suggest you look in some books before posting something like this.

--Vic--
Oluseyi
Oluseyi
Take a look at this:
int *x; 

x''s type is (int *) (a pointer to an integer) while *x''s type is int (an integer). I''ve found that explanation helps a lot of people.

[ GDNet Start Here | GDNet FAQ | MS RTFM | STL | Google ]
Thanks to Kylotan for the idea!
subnet_rx
subnet_rx
I''m reading the chapter now. I just didn''t understand that from the book. I just don''t know why you would need to know an address instead of a value.

subnet_rx
Oluseyi
Oluseyi
quote:
Original post by subnet_rx
I just don''t know why you would need to know an address instead of a value.

If you wanted a function to modify a value, you need to know the address of the variable. C/C++ pass parameters to functions by value (they make copies of the variable) except for arrays which are always passed by reference (address).

Don''t worry, it''ll come. Right now you may not understand the "whys" of many things, but just keep at it; you''ll figure it out when you start to apply the principles.

[ GDNet Start Here | GDNet FAQ | MS RTFM | STL | Google ]
Thanks to Kylotan for the idea!
subnet_rx
subnet_rx
thanks for the replies, they have been very helpful. I have one last question. I come from a background in Java. What''s the difference in using a pointer and just using a global variable? In Java, I just made a variable that had a class scope. In C++, it''s advising me to pass pointers to variables to change their values in functions.

subnet_rx
Oluseyi
Oluseyi
quote:
Original post by subnet_rx
What''s the difference in using a pointer and just using a global variable?

First off, pointers and global variables have no relation - they''re not even comparable paradigms. Even if you had a global variable in C/C++ which you wanted to modify through a function, you''d still have to pass either a pointer to that (global) variable or a reference (they''re similar, but not quite identical).

[ GDNet Start Here | GDNet FAQ | MS RTFM | STL | Google ]
Thanks to Kylotan for the idea!

Topic Locked

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

Sign in to reply to this topic.