error: cast from 'void*' to 'int' loses precision


Question

I have a function with prototype void* myFcn(void* arg) which is used as the starting point for a pthread. I need to convert the argument to an int for later use:

int x = (int)arg;

The compiler (GCC version 4.2.4) returns the error:

file.cpp:233: error: cast from 'void*' to 'int' loses precision

What is the proper way to cast this?

1
58
10/28/2009 10:28:56 PM

You can cast it to an intptr_t type. It's an int type guaranteed to be big enough to contain a pointer. Use #include <cstdint> to define it.

61
3/28/2013 4:10:06 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon