Solved

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

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Question of running the program built from sample in codeproject website 3 70
Add values of each row in an array 3 57
max float value 3 41
Precision Problem in C++ 7 32
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…
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…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
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.

809 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