Jump to content
  • Advertisement


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


Noobs + Pointers = Bad :(

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

Whats the difference?
int x;
int *pointer= &x;
int x;
int *pointer=x;
I know that the first is a pointer to the variables address but isnt the second as well? please explain, ty!

Share this post

Link to post
Share on other sites
Pointers can be tricky. In fact I am still kinda shaky with em even though I use em so much.

the & symbol means you are getting the address. So &x is something like EE09872, which is the address in memory. Your bottom piece of code shouldnt even compile. You are setting the pointer = to whatever is stored in that variable, so if x = 5, the pointer will be pointed towards 5 as an address, which the compiler shouldnt even let you do.

try this just to kind of see what happens:

int x = 5;
int *pointer = &x;

cout << pointer;
cout << *pointer;
cout << x;

the *pointer should come out same as x, since you are dereferencing what you point to.

If I am wrong in some part of this someone please correct me, been a while since I thought about it.

To aid and protect, Thus honor.

Share this post

Link to post
Share on other sites

in the second one all your giving is the variable name, not its address.

which is why you need this:

int x = 5;
int *pointer = &x; //& = address to variable

//not this

int x = 5;
int *pointer = x; // just variable name... no address

//compile that and you''ll get an error.

Share this post

Link to post
Share on other sites
This is how I 'read' my pointers. When I get confused I actually say it outloud to make sure I am doing it correctly. =)

x = 5; // "x is equal to five"

pointer = &x; // "pointer is equal to the address of x"

*pointer = x; // "The value at address pointer is equal to x"

&(*pointer) = &x; // "THe address of the value of pointer is equal to the address of x"

Also, to make it easier for me to read, I never give a value other than NULL when creating my pointers.

int *pointer = &x; // confusing!

int *pointer;
pointer = &x; // Much better!

Edited for formatting!

[edited by - psychocatt on June 10, 2004 6:57:10 PM]

Share this post

Link to post
Share on other sites
if you used int *pointer=&x;, then you''d probably end up with a really weird value in whatever pointer is pointing to. &x means the address of x, and you''re assigning that address to a regular variable (the variable that pointer is pointing to). Try:

int x=0,y=0;
int *pointer=&y;
cout << y; // Instead of getting 0, you''ll get something like 223847234.

Zorx (a Puzzle Bobble clone)
Discontinuity (an animation system for POV-Ray)

Share this post

Link to post
Share on other sites
Pointers are useful for several things. Let''s say you wanted a list of objects. Instead of creating a list of CObject, you would create a list of CObject *. That way, if you needed to create a new object, you could just do:

List[0] = new CObject;

And delete the object:

delete List[0];

You can easily use pointers this way to allocate/free memory whenever you need to, instead of letting the compiler handle it for you (when the variable goes out of scope). Which brings me to my next point:

If you want to allocate memory and have it accessible after a function goes out of scope, you can use a pointer. For example:

CObject *CreateObject()
CObject *NewObject = new CObject();
return NewObject;

Now, the memory is allocated and not freed when the function goes out of scope.

Pointers were also useful for passing large objects. When you pass a large object (i.e. the theroetical CObject), with alot of vars, you don''t want to pass it like this:

CObject BigObject;
SomeFunction( BigObject );

Because the compiler will copy the entire object to call the function. That''s memory inefficient and slow. Instead:

CObject BigObject;
SomeFunction( &BigObject );

Which makes the compiler only pass the memory location, and also allows the function to modify the data in BigObject.

Hope my long rant made pointers a little clearer .

-- Fyhuang, president, Altitude Technologies

Altitude Technologies: http://www.hytetech.com/altitude
Altitude Forums: http://s8.invisionfree.com/Altitude_Forums/

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!