Jump to content
  • Advertisement
Sign in to follow this  
Anexa85

Pointer Question

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

This question is probably a silly one but here is the code: #include "stdafx.h" #include <iostream> typedef unsigned short int USHORT; int main() { using namespace std; USHORT myAge; USHORT *pAge = &myAge; cout <<"Address of myAge: " << &myAge << endl; cout <<"Address of *page: " << &pAge << endl; return 0; } This returns: Address of myAge: 0012FF60 Address of *pAge: 0012FF54 Why are the addresses not the same?

Share this post


Link to post
Share on other sites
Advertisement
Because they're not the same. One is a pointer to myAge, the other is a pointer to pAge. (and your label is incorrect on the second line of output)

Share this post


Link to post
Share on other sites
I'm not trying to be difficult or anything, but I thought that I created a variable myAge and assigned the address of myAge to the pointer pAge. I didn't do that here?

Share this post


Link to post
Share on other sites
Quote:
Original post by Anexa85
I'm not trying to be difficult or anything, but I thought that I created a variable myAge and assigned the address of myAge to the pointer pAge. I didn't do that here?


You did, and then you printed the address of pAge rather than its value. Pointers have addresses too.

Share this post


Link to post
Share on other sites
Quote:
Original post by Anexa85
I'm not trying to be difficult or anything, but I thought that I created a variable myAge and assigned the address of myAge to the pointer pAge. I didn't do that here?


The problem is in the way you're printing out the values.

You should have written:


cout <<"Address of myAge: " << &myAge << endl;
cout <<"Address of page: " << pAge << endl; // No address-of operator here!


You want to print out the memory location that is stored inside the pointer. Not the memory location of the pointer itself.

Share this post


Link to post
Share on other sites
Quote:
Original post by fpsgamer
You should have written:


cout <<"Address of page: " << pAge << endl; // No address-of operator here!


You want to print out the memory location that is stored inside the pointer. Not the memory location of the pointer itself.


That's actually displaying the value of pAge, which is the address of myAge. I think you just made a typo. :)

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!