?
Solved

unsigned short

Posted on 1998-09-15
5
Medium Priority
?
368 Views
Last Modified: 2010-04-02
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.
0
Comment
Question by:jtm082698
  • 3
5 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 120 total points
ID: 1172779
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
 

Expert Comment

by:eric_m
ID: 1172780
Any of these should work:

temp = #

*temp = num;

num = *temp;
0
 
LVL 22

Expert Comment

by:nietod
ID: 1172781
The only work if temp is a pointer to the same type that num is.  I'm guessing that is not the case.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1172782
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
 

Author Comment

by:jtm082698
ID: 1172783
Thanks for putting me on the right track, it was the ole "can't see the forest for the trees" type thing.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question