Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Visual c++ and text files 7 60
Load and store *.pnm image file 1 75
Interoperability issue between VC++ (ocx) & VB.NET applications 7 75
Getting IP address 8 71
Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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 learn how to clear a vector as well as how to detect empty vectors in C++.

920 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

15 Experts available now in Live!

Get 1:1 Help Now