Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2000-03-27
4
Medium Priority
?
401 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

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

604 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