The best way to do this is by using the intptr_t type. This type has the same size of a pointer (not int) in every architecture. It is defined as follows :
Integer type capable of holding a value converted from a void pointer
and then be converted back to that type with a value that compares
equal to the original pointer.
So you can do something like this:
#include <stdint.h>
void *threadfunc(void *param)
{
int id = (intptr_t) param;
...
}
int i, r;
r = pthread_create(&thread, NULL, threadfunc, (void *) (intptr_t) i);
(This example code has been taken from : How to cast an integer to void pointer?)
However, there is not guarantee that the size of int is the same of the size of intptr_t , but it's really unlikely that some data is lost in the conversion process.
|