Solved

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

Posted on 2000-03-27
4
386 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
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 50 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

759 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now