?
Solved

How do u get a void * to an integer value?

Posted on 2000-03-27
4
Medium Priority
?
400 Views
Last Modified: 2008-03-04
How can I assign an integer value to  CarID? Eg. get pCarid to point to the integer value 1000?

This is in the headerfile..
typedef void *CarID;

And this is my method....
PTEC WINAPI SpellerInit (CarID *pCarid, const Variable *pVar)

Thanks.

0
Comment
Question by:nicolac
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 2

Expert Comment

by:xLs
ID: 2661760
you will need to cast void* to int*,

int *pnCarid = (int*)pCarid;

or simply

int value = *((int*) pCarid);

Cheers

0
 
LVL 22

Expert Comment

by:nietod
ID: 2661791
That woudl work if  the global CarID really does point to an int, or a value that can be treated as an int.  but it will cause unexpected problems if it does not.

What does CarID really point to?  If it really points to an int, why is id declared as a "void *" pointer?
0
 
LVL 10

Accepted Solution

by:
RONSLOW earned 200 total points
ID: 2662908
Firstly .. a CarId should be an int anyway. eg.

  typedef int CarId;

in which case you'd simply say

PTEC WINAPI SpellerInit (CarID *pCarid, const Variable *pVar) {
  ...
  *pCardid = 1000;
  ...
};

If it really MUST be a void*, and you are sure that pointers will hold int values on your platform/compiler (they are not guaranteed to), then do the following...

PTEC WINAPI SpellerInit (CarID *pCarid, const Variable *pVar) {
  ...
  *reinterpret_cast<int*>(pCardid) = 1000;
  ...
};

It is nicer to use reinterpret_cast so that your 'unusual' casting is more obvious.  Old 'C'-style casts are still supported, but are not good programming practice in general when one of the new casting operators better documents what you are trying to do (in this case re-interpretting a void pointer as an int).

Of course, once you assign an int value to a pointer like this, do NOT try to de-reference the pointer (ie use it as a pointer) as you'll more than likely cause the world to end (or at least your program to crash).

0
 

Author Comment

by:nicolac
ID: 2676958
This worked, thanks.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
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 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.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

770 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