unsigned short

I want to go back and forth from an unsigned short (temp) as a pointer to just an unsigned short (num), but ....
temp = (unsigned short*)num;
doesn't work or vice versa.
jtm082698Asked:
Who is Participating?
 
nietodConnect With a Mentor Commented:
I'm having trouble following you, but I assume that temp is declared as "unsigned short *" and num is some sort of integral type, like short.  Right?  then

unsigned short *temp = *(unsigned short **) #

that is, get a pointer to num &num), convert the pointer to be a pointer to a unsigned short pointer (pointer to "unsigned short *" is "unsigned short **"), then derference the pointer.
0
 
eric_mCommented:
Any of these should work:

temp = #

*temp = num;

num = *temp;
0
 
nietodCommented:
The only work if temp is a pointer to the same type that num is.  I'm guessing that is not the case.
0
 
nietodCommented:
Hmmm.  As I re-read I'm beginning to wonder if Eric is right.  If num is an unsigned short and temp is just a pointer to an unsigned short, then you should just do

temp = &num.

The "&" means "generate a pointer to" the item on the right.  If this is what you need, you should reject my answer and award the points to Eric.
0
 
jtm082698Author Commented:
Thanks for putting me on the right track, it was the ole "can't see the forest for the trees" type thing.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.